2016-11-22 79 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)) { 
    ... 
}