2013-02-15 65 views
5

我正在做一個powershell腳本,它在AD中創建新的域用戶帳戶,並在相關權限的文件服務器中創建主目錄。Powershell腳本爲用戶創建主文件夾並設置權限

我的問題是我無法獲得權限設置。

在下面的代碼中,my_fileServer是文件服務器名稱; sso表示單點登錄ID,在下面的測試代碼中設置爲「user9999」。

任何幫助,非常感謝!

Set-Variable homeDir -option Constant -value "\\my_fileServer\Users" 
Set-Variable sso -option Constant -value "user9999" 

# If the folder for the user does not exist, make a new one and set the correct permissions. 
if ((Test-Path "$homeDir\$sso") -eq $false) 
{ 
    try 
    { 
     $NewFolder = New-Item -Path $homeDir -Name $sso -ItemType "Directory" 
     $Rights = [System.Security.AccessControl.FileSystemRights]"FullControl,Modify,ReadAndExecute,ListDirectory,Read,Write" 
     $InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::None 
     $PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None 
     $objType =[System.Security.AccessControl.AccessControlType]::Allow 
     $objUser = New-Object System.Security.Principal.NTAccount "my_full_domain_name\$sso" 
     $objACE = New-Object System.Security.AccessControl.FileSystemAccessRule ` 
       ($objUser, $Rights, $InheritanceFlag, $PropagationFlag, $objType) 
     $ACL = get-acl -Path $NewFolder 
     $ACL.AddAccessRule($objACE) 
     $objReturn = Set-ACL -Path "$homeDir\$sso" -AclObject $ACL 
    $objReturn 
    } 
    catch 
    { 
     $msg = $_ 
     $msg 
    } 
} 

主文件夾被創建好,但是當我檢查用戶的權限時,沒有勾選框。 enter image description here

+0

Set-Acl不返回一個值。 – 2013-02-15 11:25:00

回答

6

問題是你的無知。您不允許在子文件夾和文件(他擁有的文件夾中的項目)上繼承權限。這就是爲什麼您在基本安全性窗口中看不到權限(僅「特權」)的原因。如果您打開「高級安全設置」,您將看到用戶完全控制此文件夾,而不是內容。只要您爲CREATOR OWNER添加權限(使用繼承),以便所有者獲得對項目的訪問權限,我認爲您會沒事的。然而,你可能已經立即修復這樣的:

$InheritanceFlag = @([System.Security.AccessControl.InheritanceFlags]::ContainerInherit,[System.Security.AccessControl.InheritanceFlags]::ObjectInherit) 

除非有特殊的要求,你應該給用戶在他的文件夾(完全繼承)的完全訪問權限。與固定繼承完整的解決方案(我也清理了您的Set-ACL路徑和刪除不必要返回object):

try 
{ 
    $NewFolder = New-Item -Path $homeDir -Name $sso -ItemType "Directory" 
    $Rights = [System.Security.AccessControl.FileSystemRights]"FullControl,Modify,ReadAndExecute,ListDirectory,Read,Write" 
    $InheritanceFlag = @([System.Security.AccessControl.InheritanceFlags]::ContainerInherit,[System.Security.AccessControl.InheritanceFlags]::ObjectInherit) 
    $PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None 
    $objType =[System.Security.AccessControl.AccessControlType]::Allow 
    $objUser = New-Object System.Security.Principal.NTAccount "my_full_domain_name\$sso" 
    $objACE = New-Object System.Security.AccessControl.FileSystemAccessRule ` 
      ($objUser, $Rights, $InheritanceFlag, $PropagationFlag, $objType) 
    $ACL = Get-Acl -Path $NewFolder 
    $ACL.AddAccessRule($objACE) 
    Set-ACL -Path $NewFolder.FullName -AclObject $ACL 
} 
+0

非常感謝你!現在我明白爲什麼.. – user1866880 2013-02-15 15:51:52

0

所有的權限設置是否正確爲'Special Permmissions',你可以Advanced請在點擊,並期待在「授權」標籤。

2

我遺憾的是不能投票,但我上面(Graimer和CB),實際的答案都同意的答案是兩者的結合。
- 您需要檢查「高級」窗口中的權限
- 即使您的代碼「有效」,沒有繼承,您的用戶將無法在您分配給他們的文件夾中做很多事情。

0

保持簡單,使用更少的操作......您錯過的是SetAccessRuleProtection函數。

下面是可以給你想要的刻度的代碼。

if (-not (Test-Path "$homeDir\$sso")) 
{ 
    $acl = Get-Acl (New-Item -Path $homedir -Name $sso -ItemType Directory) 

    # Make sure access rules inherited from parent folders. 
    $acl.SetAccessRuleProtection($false, $true) 

    $ace = "$domain\$sso","FullControl", "ContainerInherit,ObjectInherit","None","Allow" 
    $objACE = New-Object System.Security.AccessControl.FileSystemAccessRule($ace) 
    $acl.AddAccessRule($objACE) 
    Set-ACL -Path "$homeDir\$sso" -AclObject $acl 

}