Advertisement
wetyukmnbxc

Untitled

Mar 4th, 2023 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Write a .net and powershell script to manipulate folder and acl.
  2.  
  3. Here's an example .NET and PowerShell script to manipulate folder and ACL:
  4.  
  5. .NET:
  6.  
  7. csharp
  8. using System;
  9. using System.IO;
  10. using System.Security.AccessControl;
  11.  
  12. class Program
  13. {
  14.    static void Main(string[] args)
  15.    {
  16.        string folderPath = @"C:\example\folder";
  17.        string user = "DOMAIN\username";
  18.        FileSystemAccessRule rule = new FileSystemAccessRule(user, FileSystemRights.FullControl, AccessControlType.Allow);
  19.  
  20.        // Create the folder if it doesn't exist
  21.         if (!Directory.Exists(folderPath))
  22.         {
  23.             Directory.CreateDirectory(folderPath);
  24.         }
  25.  
  26.         // Get the folder's ACL
  27.        DirectorySecurity security = Directory.GetAccessControl(folderPath);
  28.  
  29.        // Add the new rule to the ACL
  30.        security.AddAccessRule(rule);
  31.  
  32.        // Set the folder's new ACL
  33.         Directory.SetAccessControl(folderPath, security);
  34.     }
  35. }
  36.  
  37.  
  38. PowerShell:
  39.  
  40. powershell
  41. $folderPath = "C:\example\folder"
  42. $user = "DOMAIN\username"
  43. $rule = New-Object System.Security.AccessControl.FileSystemAccessRule($user, "FullControl", "Allow")
  44.  
  45. # Create the folder if it doesn't exist
  46. if (!(Test-Path $folderPath))
  47. {
  48.     New-Item -ItemType Directory -Path $folderPath
  49. }
  50.  
  51. # Get the folder's ACL
  52. $security = Get-Acl $folderPath
  53.  
  54. # Add the new rule to the ACL
  55. $security.AddAccessRule($rule)
  56.  
  57. # Set the folder's new ACL
  58. Set-Acl $folderPath $security
  59.  
  60.  
  61. Both scripts create a folder at `C:\example\folder` if it doesn't exist, and add a new ACL rule to give the user `DOMAIN\username` full control over the folder.  Share
  62. Start chat...
  63. chatgptweb.org realistic unlimited experience of AI.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement