簡體   English   中英

使用不同區域性的PowerShell設置文件夾權限

[英]Set folder permissions with PowerShell in different cultures

我正在嘗試創建一個PowerShell腳本,該腳本在不同的區域性中授予NETWORK SERVICE文件夾權限。 主要問題是,盡管在所有Windows安裝中都提供了NETWORK SERVICE,但在不同的地區有不同的名稱,我不知道該如何處理。

這是我正在使用的腳本:

$appPath = "C:\SomeFolder"

$Acl = (Get-Item $appPath).GetAccessControl('Access') 

$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule("NETWORK SERVICE", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")

$Acl.SetAccessRule($Ar)

Set-Acl $appPath $Acl

現在,此腳本在英語版本的Windows上運行正常。 但是,當嘗試在德語版本的Windows上運行它時,出現以下錯誤消息(從德語翻譯):

Exception calling "SetAccessRule" with "1" argument(s): "Some or all identity
references could not be translated."
At C:\Experimental Files\GrantFolderPermissions.ps1:7 char:1
+ $Acl.SetAccessRule($Ar)
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : IdentityNotMappedException

我如何最好地處理此問題,以便此腳本可以獨立地工作在文化環境中?

使用眾所周知的SID確定帳戶名:

$sid = [Security.Principal.SecurityIdentifier]'S-1-5-20'
$acct = $sid.Translate([Security.Principal.NTAccount]).Value

$ace = New-Object Security.AccessControl.FileSystemAccessRule($acct, 'FullControl', 'ContainerInherit,ObjectInherit', 'None', 'Allow')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM