PowerShell and foreach loop

Asked by At

I'm working a PowerShell script to change the permissions for the "builtin users" group basically I need to add "modify" to one folder. I need to do this to about 400 systems. My computer names are not showing up.

$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
}

I expect the code to run through all 400 names in a text file and change permissions.

1 Answers

0
HO LI Pin On

1st line was wrong....

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

do something like this.. $computernames= Get-Content -Path c:\computernames.txt

then you can loop the 400 computer and do require logic

enter image description here

also make sure you properly built the $path value , you will need to use + to join string...