gpt4 book ai didi

powershell - 将对象复制到具有更改值的新对象

转载 作者:行者123 更新时间:2023-12-03 00:14:01 31 4
gpt4 key购买 nike

需要获取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 "BUILTIN\Users") {
"found"
$h += @($_.Access.IdentityReference.Value.Replace("BUILTIN\Users", "new\name"))
} else {
$h += @($_)
}
}
$h.Access

我有很多方法可以做到这一点,而我能够获得的最好的方法就是找到并替换目标值,但丢失了其余的原始对象。

编辑:尝试此代码时的:
$acl = Get-Acl -Path 'C:\Temp'

$ace = $acl.Access | Where-Object { $_.IdentityReference -eq 'BUILTIN\Users' }
$newAce = New-Object System.Security.AccessControl.FileSystemAccessRule (
'new\name',
$ace.FileSystemRights,
$ace.InheritanceFlags,
$ace.PropagationFlags,
$ace.AccessControlType
)
$acl.RemoveAccessRule($ace)
$acl.AddAccessRule($newAce)

Set-Acl -Path 'C:\Temp' -AclObject $acl

我收到以下错误:
Exception calling "AddAccessRule" with "1" argument(s): "Some or all identityreferences could not be translated."At line:12 char:1+ $acl.AddAccessRule($newAce)+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException     + FullyQualifiedErrorId : IdentityNotMappedException

Edit2: New script, but not quite there yet:

$Ssid = "S-1-5-21-2214593661-3374179426-1523454523-1133"
$Tsid = "S-1-5-21-2227185791-3254421074-497073356-1005"
$Spath = "\\clw01\dept\CLW_Site"
$Tpath = "\\clw03\dept\CLW_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字符:1
+ $ acl.AddAccessRule($ newAce)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo:未指定:(:) [],MethodInvocationException
+ FullyQualifiedErrorId:SystemException

最佳答案

您不能替换现有ACE上的标识引用,而需要用新的ACE替换整个ACE。

$acl = Get-Acl -Path 'C:\Temp'

$ace = $acl.Access | Where-Object { $_.IdentityReference -eq 'BUILTIN\Users' }
$newAce = New-Object System.Security.AccessControl.FileSystemAccessRule (
'new\name',
$ace.FileSystemRights,
$ace.InheritanceFlags,
$ace.PropagationFlags,
$ace.AccessControlType
)
$acl.RemoveAccessRule($ace)
$acl.AddAccessRule($newAce)

Set-Acl -Path 'C:\Temp' -AclObject $acl

关于powershell - 将对象复制到具有更改值的新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41211831/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com