How to create folder, share and apply NTFS permissions

后端 未结 1 925
既然无缘
既然无缘 2021-01-20 13:15

Being a total novice of Powershell I am trying to put together a script using the below script from various TechNet script examples:

$FolderPath = \'c:\\fold         


        
相关标签:
1条回答
  • 2021-01-20 13:59

    You could have solved this yourself if you had tried to search. I've created an answer for share permissions earlier here, and NTFS permissions are easily found too. Try this:

    #Local path
    $FolderPath = 'c:\folder'
    
    $Shares=[WMICLASS]'WIN32_Share'
    #Share name
    $ShareName='Home$'
    
    #Create folder
    New-Item -type directory -Path $FolderPath
    
    #Create share rights
    
    #Define a trustee (person/group to give access right)
    $trustee = ([wmiclass]‘Win32_trustee’).psbase.CreateInstance()
    $trustee.Domain = "NT Authority"
    $trustee.Name = “Authenticated Users”
    
    #Define an access control entry (permission-entry)
    $ace = ([wmiclass]‘Win32_ACE’).psbase.CreateInstance()
    #Modify-rights
    $ace.AccessMask = 1245631
    #Inheritance for folders and files
    $ace.AceFlags = 3
    $ace.AceType = 0
    #Assign rights to Authenticated users ($trustee)
    $ace.Trustee = $trustee
    
    $trustee2 = ([wmiclass]‘Win32_trustee’).psbase.CreateInstance()
    $trustee2.Domain = "BUILTIN"  #Or domain name
    $trustee2.Name = “Administrators”
    
    $ace2 = ([wmiclass]‘Win32_ACE’).psbase.CreateInstance()
    #Full control
    $ace2.AccessMask = 2032127
    $ace2.AceFlags = 3
    $ace2.AceType = 0
    #Assign rights to Administrators ($trustee2)
    $ace2.Trustee = $trustee2
    
    #Create ACL/security descriptor. This is the security-definitions that you set on the share.
    $sd = ([wmiclass]‘Win32_SecurityDescriptor’).psbase.CreateInstance()
    #Specify that a DACL (ACL/security/permissions) are available, so the share isn't set to full access for everyone
    $sd.ControlFlags = 4
    #Add our rules
    $sd.DACL = $ace, $ace2
    #Set Administrators ($trustee2) as owner and group of ITEM (will be the share)
    $sd.group = $trustee2
    $sd.owner = $trustee2
    
    #Create share with the security rules
    $shares.create($FolderPath, $ShareName, 0, 100, "Description", "", $sd) | Out-Null
    
    #Get NTFS permissiongs
    $Acl = Get-Acl $FolderPath
    #Disable inheritance and clear permissions
    $Acl.SetAccessRuleProtection($True, $False)
    #Define NTFS rights
    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule('Administrators','FullControl','ContainerInherit, ObjectInherit', 'None', 'Allow')
    $Acl.AddAccessRule($rule)
    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule('SYSTEM','FullControl','ContainerInherit, ObjectInherit', 'None', 'Allow')
    $Acl.AddAccessRule($rule)
    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule("Authenticated Users",@("ReadData", "AppendData", "Synchronize"), "None", "None", "Allow")
    $Acl.AddAccessRule($rule)
    $rule = New-Object System.Security.AccessControl.FileSystemAccessRule('CREATOR OWNER','FullControl','ContainerInherit, ObjectInherit', 'InheritOnly', 'Allow')
    $Acl.AddAccessRule($rule)
    
    #Save ACL changes (NTFS permissions)
    Set-Acl $FolderPath $Acl | Out-Null
    #Show ACL so user can verify changes
    Get-Acl $FolderPath  | Format-List
    
    0 讨论(0)
提交回复
热议问题