2016-07-26 71 views
0

我正在写一个函数,将从路径A到ACL的ACL等同于路径B(路径A也可以是服务器B上的服务器A和路径B)。几乎所有内容都按预期工作,用户被部署到目标路径,但FileSystemRights不会被部署,即使我在函数中硬编码“FullControl”。FileSystemRights没有部署

我从来没有在PowerShell中使用ACL合作过,并复制了我的大部分代码从这里:https://technet.microsoft.com/en-us/library/ff730951.aspx?f=255&MSPPError=-2147217396

为什么我FileSystemRights没有得到部署?

Process { 
# get ACL from source path 
    $gacl = get-acl $SourcePath | select -ExpandProperty Access | % { 
    $ErrorActionPreference = "SilentlyContinue" 
    [string]$user = ($_.IdentityReference).Value.split('\')[1] 
    [string]$AccessType = $_.AccessControlType 
    $FSRights = $_.FileSystemRights 

    if (!$user) { Write-Warning "User not found. Skipping ACL settings for this user. Username: $(($_.IdentityReference).Value)`n"} 
    else{ 
    # Create ACL Object 
    $colRights = [System.Security.AccessControl.FileSystemRights]"FullControl" 
    $InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::None 
    $PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None 
    $objType =[System.Security.AccessControl.AccessControlType]$AccessType 
    $objUser = New-Object System.Security.Principal.NTAccount($user) 
    $objACE = New-Object System.Security.AccessControl.FileSystemAccessRule($objUser, $colRights, $InheritanceFlag, $PropagationFlag, $objType) 

    # Set the ACL 
    Write-Host "Setting ACL for User: $User on $DestinationPath" -ForegroundColor Green 
    $objACL = get-acl $DestinationPath 
    $ErrorActionPreference = "Stop" 
    Try { 
     $objACL.AddAccessRule($objACE) 
     $sacl = set-acl $DestinationPath $objACL 
     Write-Host "Success!`n" -ForegroundColor Green 
    } Catch { 
     Write-Host "Failed! ErrorMessage:" -ForegroundColor Red 
     $_.Exception.Message 
    }} 
}} 

回答

0

我放弃了这一点,并决定只是使用一个模块,为我做的伎俩。我使用了以下模块:https://gallery.technet.microsoft.com/scriptcenter/1abd77a5-9c0b-4a2b-acef-90dbb2b84e85

这就是最终的功能。非常容易:

function Equate-ACL { 
param(
    [Parameter(Mandatory=$true,Position=0)] 
    [string]$SourcePath, 
    [Parameter(Mandatory=$true,Position=1)] 
    [string]$DestinationPath 
) 
    if(!(get-module NTFSSecurity)) { import-module NTFSSecurity } 
    $ErrorActionPreference = "Stop" 
    Try { 
     $SourcePath | Get-NTFSAccess | Add-NTFSAccess $DestinationPath 
    } Catch { 
     Write-Warning "Konnte ACL von $Sourcepath nicht auf $Destinationpath setzen." 
    } 
}