2016-11-22 71 views
0

这里是代码我必须为我的用户创建一个目录。我想知道如何跳过ACL部分,如果目录已经存在。如果该目录存在跳过权限,如果目录存在

#Search AD for Elem Students 

$elem = Get-ADUser -Filter 'company -eq "1479"' 
$path = "\\hs-ss\students\elem" 
foreach ($user in $elem) 
{ 
    $Username = $User.SamAccountName 

    #Create user directory of Storage Server 

    New-Item -Path $path -Name $Username -ItemType "directory" -Force | Out-Null 
    $ACL = Get-Acl "$path\$Username" 
    $ACL.SetAccessRuleProtection($true, $false) 
    $ACL.Access | ForEach { [Void]$ACL.RemoveAccessRule($_) } 
    $ACL.AddAccessRule((New-Object System.Security.AccessControl.FileSystemAccessRule("$NTDomain\Domain  Admins", "FullControl", "ContainerInherit, ObjectInherit", "None", "Allow"))) 
    $ACL.AddAccessRule((New-Object System.Security.AccessControl.FileSystemAccessRule("$NTDomain\$username", "Modify", "ContainerInherit, ObjectInherit", "None", "Allow"))) 
    Set-Acl "$Path\$username" $ACL 
} 

回答

1

检查:

if (-not (Test-Path -LiteralPath "$path\$Username" -Type Container)) { 
    ... 
}