代码之家  ›  专栏  ›  技术社区  ›  Kira Resari

在不同区域性中使用PowerShell设置文件夹权限

  •  1
  • Kira Resari  · 技术社区  · 8 年前

    我正在尝试创建PowerShell脚本,该脚本将文件夹权限授予不同区域性上的网络服务。主要的问题是,网络服务虽然存在于所有Windows安装中,但在不同的文化中有不同的名称,我不知道如何处理这个问题。

    以下是我使用的脚本:

    $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
    

    我如何才能最好地处理这个问题,使这个脚本能够独立于文化环境工作?

    1 回复  |  直到 8 年前
        1
  •  1
  •   Ansgar Wiechers    8 年前

    使用 well-known 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')
    
    推荐文章