需要获取ACL对象并替换Access.IdentityReference.Value
并保持其余对象完整,以便我可以将Set-Acl
应用于另一个系统。
$acl = Get-Acl -Path "C:Temp"
$h = New-Object -TypeName PSObject
ForEach-Object -InputObject $acl -Process {
if ($_.Access.IdentityReference.Value -contains "BUILTINUsers") {
"found"
$h += @($_.Access.IdentityReference.Value.Replace("BUILTINUsers", "newname"))
} else {
$h += @($_)
}
}
$h.Access
我有很多方法可以做到这一点,而我能获得的最好的方法是找到和替换目标值,但失去了原始对象的其余部分。
编辑:当我尝试此代码时:
$acl = Get-Acl -Path 'C:Temp'
$ace = $acl.Access | Where-Object { $_.IdentityReference -eq 'BUILTINUsers' }
$newAce = New-Object System.Security.AccessControl.FileSystemAccessRule (
'newname',
$ace.FileSystemRights,
$ace.InheritanceFlags,
$ace.PropagationFlags,
$ace.AccessControlType
)
$acl.RemoveAccessRule($ace)
$acl.AddAccessRule($newAce)
Set-Acl -Path 'C:Temp' -AclObject $acl
我会收到以下错误:
异常调用" AddAccessrule",带有" 1"参数:"某些或所有身份参考无法翻译。"在线:12 char:1 $ acl.addaccessrule($ newace) ~~~~~~~~~~~~~~~~~~~~~~~~ categoryInfo:未指定:(:) [],methodInvocation exception 完全qualififiedErrid:IdentityNotMappedException
edit2:新脚本,但尚不完全存在:
$Ssid = "S-1-5-21-2214593661-3374179426-1523454523-1133"
$Tsid = "S-1-5-21-2227185791-3254421074-497073356-1005"
$Spath = "\clw01deptCLW_Site"
$Tpath = "\clw03deptCLW_Site"
$acl = Get-Acl -Path $Spath
$ace = $acl.Access | Where-Object { $_.IdentityReference -eq $Ssid }
$newAce = New-Object System.Security.AccessControl.FileSystemAccessRule (
$Tsid,
$ace.FileSystemRights,
$ace.InheritanceFlags,
$ace.PropagationFlags,
$ace.AccessControlType
)
$acl.RemoveAccessRule($ace)
$acl.AddAccessRule($newAce)
Set-Acl -Path $Tpath -AclObject $acl
我会收到以下错误:
异常调用" 1"参数的" addaccessrule":"信任关系在主要领域和受信任的领域之间失败了。"在线:9 char:1 $ acl.addaccessrule($ newace) ~~~~~~~~~~~~~~~~~~~~~~~~ categoryInfo:未指定:(:) [],methodInvocation exception 完全qualififiedErrid:SystemException
您无法替换现有ACE上的身份参考,您需要用新ACE替换整个ACE。
$acl = Get-Acl -Path 'C:Temp'
$ace = $acl.Access | Where-Object { $_.IdentityReference -eq 'BUILTINUsers' }
$newAce = New-Object System.Security.AccessControl.FileSystemAccessRule (
'newname',
$ace.FileSystemRights,
$ace.InheritanceFlags,
$ace.PropagationFlags,
$ace.AccessControlType
)
$acl.RemoveAccessRule($ace)
$acl.AddAccessRule($newAce)
Set-Acl -Path 'C:Temp' -AclObject $acl