November 16th, 2014

PowerTip: Continue a Windows PowerShell Script After Restart

Doctor Scripto
Scripter

Summary: Learn how to continue a script after a remote system restart by using Windows PowerShell.

Hey, Scripting Guy! Question I am configuring MPIO to automatically claim the iSCSI Bus Type, which requires a restart. However, I want to continue
           automating afterward, so how do I continue my script from that point?

Hey, Scripting Guy! Answer Introduced in Windows PowerShell 4.0, you can use Restart-Computer with the Wait and For parameters.
           By default the Restart-Computer cmdlet uses DCOM as the protocol to perform the restart, so if WSMan is your
           only management protocol, you’ll have to use the Protocol parameter and specify WSMan (as the following
           example illustrates). The Wait parameter is used to “Wait” “For” “PowerShell” to respond before continuing
           further in the script.

Invoke-Command -ScriptBlock {Enable-MSDSMAutomaticClaim -BusType iSCSI} -ComputerName SVR01
Restart-Computer -Protocol WSMan -Wait -For PowerShell -ComputerName SVR01
Invoke-Command -ScriptBlock {Set-Service -Name MSiSCSI -StartupType Automatic} -ComputerName SVR01

Note  Today’s PowerTip is supplied by Microsoft PFE, Brian Wilhite.

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.