PowerTip: Continue a Windows PowerShell Script After Restart


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.

Comments (3)

  1. SteveD says:

    Which user context the script will run under following the reboot?

  2. CommentBot1 says:

    SteveD – this looks like a script that will restart *another computer*, and wait for it to restart before continuing. The script on the first computer never stops running or changes user accounts.

  3. Sakai says:

    I wanted to run this command from a SCCM R2 2012. This script seems to require the computer name, the names of the computer will differ. How would I make a blanket statement?

Skip to main content