Pages

Sunday, 11 November 2018

Do ... While command

The following PowerShell code is designed to be used as part of a larger script. My experience of Do ... While has typically been with installing/uninstalling applications where a script has exited before certain Processes have finished. This snippet creates a 10 second delay if the specified Process is found to be still running.

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
#A variable with the name of the Process is created
$ProcessName = "name_here"

Do {
    #Inside the 'Do' we have the action that should be taken if the Process is detected (so Sleep in this instance)
    Start-Sleep -Seconds 10
}

#Looks for the Process to see if it is running. Hides any errors such as the Process not running, so the script can continue
While (Get-Process $ProcessName -ErrorAction SilentlyContinue)
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

Do ... While command.ps1

No comments:

Post a comment