如何使用PowerShell将一个文件夹的ACL附加到另一个文件夹?
我尝试使用Get-Acl
和Set-Acl
,但是"当命令完成时,安全描述符......一模一样。
这会导致我在下面的\destshare
失去destAdministrators: Full Control
和其他默认共享权限。
Get-Acl '\sourceshare' | Set-Acl '\destshare'
设置-ACL - 示例 |Microsoft文档
示例 1:将安全描述符从一个文件复制到另一个文件
$DogACL = Get-Acl -Path "C:Dog.txt" Set-Acl -Path "C:Cat.txt" -AclObject $DogACL
这些命令从 Dog.txt 文件到 Cat.txt 文件的安全描述符。当 命令完成,狗.txt和猫的安全描述符.txt 文件是相同的。
如何将\sourceshare
的 ACL 附加到\destshare
?
如果要添加额外的ACL而不是替换它们,则必须在设置目标ACL之前对其进行修改。
以下是我在实时共享上尝试之前如何设置以对其进行测试的方法:
# Make folders and shares on the machine you'd like to test on
New-Item -Path C:UsersTestUserDocumentsShare1 -Type Directory -Force
New-Item -Path C:UsersTestUserDocumentsShare2 -Type Directory -Force
New-SmbShare -Path "C:UsersTestUserDocumentsShare1" -Name SourceShare
New-SmbShare -Path "C:UsersTestUserDocumentsShare2" -Name DestinationShare
现在,花点时间手动更改共享上的 ACL,以便在将其从源复制到目标时判断它是否有效。然后继续下面:
# Set network share path variables
$SourceShare = "\GLaDOSSourceShare"
$DestinationShare = "\GLaDOSDestinationShare"
# Set ACL variables
$SourceAcl = Get-Acl $SourceShare
$DestinationAcl = Get-Acl $DestinationShare
# Add all the source ACL's to the destination ACL
$SourceAcl.Access | foreach {$DestinationAcl.AddAccessRule($_)}
# Invoke the command on the computer using local path since network path does not seem to work
Invoke-Command -ComputerName GLaDOS -ScriptBlock {$LocalPath = (Get-SmbShare -Name DestinationShare).Path ; Set-Acl $LocalPath $Using:DestinationAcl}
显然,您需要分别将TestUser和GLaDOS替换为用户名和计算机名。
附言在使用Invoke-Command
之前,我尝试了一个不太复杂的选项,并得到了一个身份验证错误:
PS C:> Set-Acl $DestinationShare $DestinationAcl
Set-Acl : Attempted to perform an unauthorized operation.
还是没弄清楚那个。我应该拥有所需的所有权限。