Acerbity
Acerbity

Reputation: 527

Powershell script with multiple loops stopping after one

I am working on a script to set file permissions using the ShareUtils module and for some reason it is not continuing to my second and third stage loops. Not sure why?

$input | Where-Object {$_.AccessMask -like 2032127} | Foreach-Object {
Get-Share -Name $_.Name | Add-SharePermission $_.User Allow FullControl | Set-Share
}

$input | Where-Object {$_.AccessMask -like 1245631} | Foreach-Object {
Get-Share -Name $_.Name | Add-SharePermission $_.User Allow Change | Set-Share
}

$input | Where-Object {$_.AccessMask -like 1179817} | Foreach-Object {
Get-Share -Name $_.Name | Add-SharePermission $_.User Allow Read | Set-Share
}

Upvotes: 1

Views: 187

Answers (1)

Shay Levy
Shay Levy

Reputation: 126932

Try to reset $input. Once $input is called it processes all of its elements and moves forward until it gets to the last item.

$input.reset()

UPDATE

You could also rewrite the solution, an example would be:

$input | Foreach-Object {

    $perm = switch($_.AccessMask)
    {
        2032127 {'FullControl'; break}
        1245631 {'Change'; break}
        1179817 {'Read'; break}
    }

    if($perm)
    {
       Get-Share -Name $_.Name | Add-SharePermission $_.User Allow $perm | Set-Share        
    }       

}

Upvotes: 1

Related Questions