带附加Set-Acl

问题描述 投票:0回答:1

如何使用PowerShell将一个文件夹的ACL附加到另一文件夹?

我尝试使用Get-AclSet-Acl,但是“当命令完成时,安全描述符..是相同的。”

这将导致我下面的\\dest\share失去dest\Administrators: Full Control和其他默认共享权限。

Get-Acl '\\source\share' | Set-Acl '\\dest\share'

Set-Acl - Examples | Microsoft Docs

示例1:将安全描述符从一个文件复制到另一个文件

$DogACL = Get-Acl -Path "C:\Dog.txt"
Set-Acl -Path "C:\Cat.txt" -AclObject  $DogACL

这些命令从安全性描述符的值中复制值Dog.txt文件到Cat.txt文件的安全描述符。当。。。的时候命令完成后,Dog.txt和Cat.txt的安全描述符文件是相同的。

如何将\\source\share的ACL附加到\\dest\share

powershell acl access-control
1个回答
0
投票

如果要添加额外的ACL而不是替换它们,则必须在设置目标ACL之前对其进行修改。

这里是在实时共享上尝试之前进行测试的脚本:

# Make folders and shares
New-Item -Path C:\Users\TestUser\Documents\Share1 -Type Directory -Force
New-Item -Path C:\Users\TestUser\Documents\Share2 -Type Directory -Force
New-SmbShare -Path "C:\Users\TestUser\Documents\Share1" -Name SourceShare
New-SmbShare -Path "C:\Users\TestUser\Documents\Share2" -Name DestinationShare

# Set network share path variables
$SourceShare = "\\GLaDOS\SourceShare"
$DestinationShare = "\\GLaDOS\DestinationShare"

# 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 locally 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。


P.S。在我使用Invoke-Command之前,我尝试了一个不太复杂的选项并收到了身份验证错误:

PS C:\> Set-Acl $DestinationShare $DestinationAcl
Set-Acl : Attempted to perform an unauthorized operation.

[我尝试使用$DestinationAcl.SetAccessRuleProtection($False, $False),在某些情况下,我发现它可以解决此问题,但对我而言却没有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.