更改远程PC上的文件夹权限

时间:2020-10-16 08:32:45

标签: powershell

我有一个txt文件,里面有很多PC名称。我需要为本地组“用户”->读取和写入更改每台PC上特定文件夹的文件夹权限。文件夹总是一样。

我有以下内容。如果我只将一台计算机名放在txt文件中,则效果很好,但如果我将两台或更多台计算机名,则无法工作。

这就是我所拥有的

$file = get-content -path "C:\pcnames.txt"

$acl = get-acl -path "$file\C$\testfolder1" 
$new = "users","write, read","ContainerInherit,ObjectInherit","None","Allow" 
$accessRule = new-object System.Security.AccessControl.FileSystemAccessRule $new 
$acl.SetAccessRule($accessRule) 
$acl | Set-Acl "$file\C$\testfolder1"

1 个答案:

答案 0 :(得分:0)

这是因为如果有多行,Get-Content将返回一个数组。因此,您需要遍历$file数组中的每个元素。

您的代码将如下所示:

$file = get-content -path "C:\pcnames.txt"

foreach($pc in $file) {
    $acl = get-acl -path "$pc\C$\testfolder1" 
    $new = "users","write, read","ContainerInherit,ObjectInherit","None","Allow" 
    $accessRule = new-object System.Security.AccessControl.FileSystemAccessRule $new 
    $acl.SetAccessRule($accessRule) 
    $acl | Set-Acl "$pc\C$\testfolder1"
}