首页 > 解决方案 > PowerShell 和 foreach 循环

问题描述

我正在使用 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 个名称并更改权限。

标签: powershell

解决方案


第一行写错了......

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

做这样的事情.. $computernames= Get-Content -Path c:\computernames.txt

然后您可以循环 400 计算机并且确实需要逻辑

在此处输入图像描述

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


推荐阅读