frodo
frodo

Reputation: 1063

How to pause a foreach PowerShell loop

I have created this simple script below to loop through a file containing a list of users and apply quota rules for each user in the file.

It works, but during processing it fails sometimes as I believe the large number of users in the text file blocks access at times. Is there a way to get the loop to pause for 5 - 10 seconds after each iteration of the foreach loop or after five iterations of the foreach loop until the filer has updated those quotas completely and then move on to the next user in the file?

ForEach ($adname in Get-Content "C:\Users\mick\Desktop\scripts\staff.txt")
{
    Invoke-NcSsh -Command "volume quota policy rule create -vserver FS-ONE -policy-name default -volume data -type user -target $adname -qtree """" -user-mapping off -disk-limit 3GB -soft-disk-limit 2GB -threshold 3GB"
}

Start-NcQuotaResize -vserver FS-ONE -volume data

Upvotes: 2

Views: 11023

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 175085

Use a counter and Start-Sleep:

$UserCount = 0
foreach($adname in Get-Content "C:\Users\mick\Desktop\scripts\staff.txt")
{
    # Sleep 3 seconds every 5 runs
    if(++$UserCount % 5 -eq 0) 
    {
        Start-Sleep -Seconds 3
    }

    Invoke-NcSsh -Command "volume quota policy rule create -vserver FS-ONE -policy-name default -volume data -type user -target $adname -qtree """" -user-mapping off -disk-limit 3GB -soft-disk-limit 2GB -threshold 3GB"
}

Upvotes: 5

Related Questions