【问题标题】:PowerShell and foreach loopPowerShell 和 foreach 循环
【发布时间】:2019-09-19 12:01:04
【问题描述】:

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

【问题讨论】:

  • foreach($computersystem in $computer),您已将 get-content 导入$computer 并且路径看起来不正确,应该是类似“C:\ComputerNames.txt”的字符串
  • $path = "\\$computer\C`$\Program Files (x86)\Directory1\Directory2" 移动到foreach 循环体中。
  • 你的路径没有被引用。
  • 你更新的代码还是不行吗?它实际上是做什么的?请提供更多详细信息。

标签: powershell


【解决方案1】:

第一行错了....

$computer = Get-Content -Path c:\computernames.txt

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

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

还要确保你正确构建了 $path 值,你需要使用 + 来加入字符串...

【讨论】:

  • 感谢大家的帮助!我不确定第一行发生了什么我不应该剪切并粘贴我的名字更改:(在循环内移动路径似乎有助于@HO LI Pin我不确定你的意思是“你需要使用 + 加入字符串..." 这是正确的 $path = "\\$Computer\C$\Program Files (x86)\Directory1\Directory2" 吗?它似乎正在工作我需要做更多的测试@Ansgar Wiechers该脚本基本上将 BUILTIN\USERS 组的“修改”权限添加到一个程序文件夹 d。
猜你喜欢
  • 1970-01-01
  • 2018-03-13
  • 2021-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-17
相关资源
最近更新 更多