PowerShell和foreach循环

时间:2019-04-30 19:22:03

标签: powershell

我正在使用PowerShell脚本来更改“内置用户”组的权限,基本上,我需要将“修改”添加到一个文件夹中。我需要对约400个系统执行此操作。我的计算机名称未显示。

$computer = Get-Content -Path c:\computernames.txt
$user = "BUILTIN\Users" 
$Rights = "Modify","Synchronize" 
$InheritSettings = "Containerinherit, ObjectInherit"
$PropogationSettings = "None" 
$RuleType = "Allow" 

foreach ($computer in $computernames) {
    $path = "\\$computer\C$\Program Files (x86)\Directory1\Directory2"
    $acl = Get-Acl $path
    $perm = $user, $Rights, $InheritSettings, $PropogationSettings, $RuleType
    $rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $perm
    $acl.SetAccessRule($rule)
    $acl | Set-Acl -Path $path
}

我希望代码能在一个文本文件中贯穿所有400个名称并更改权限。

1 个答案:

答案 0 :(得分:0)

第一行是错误的。...

$ computer =获取内容-路径c:\ computernames.txt

做这样的事情。 $ computernames =获取内容-路径c:\ computernames.txt

然后您可以循环运行400台计算机并需要逻辑

enter image description here

还要确保您正确构建了$ path值,您将需要使用+来连接字符串...

相关问题