Neudesic Blogs

Passion for Innovation

A PowerShell Script to Restart Computer and Continue Execution

So originally I had set out to write an article about setting up a SharePoint 2010 development environment.  I know it’s been written about ad nauseam but thought it would be a great way to get my feet wet in the blogging world.  Anyway as usual I got myself distracted so this article is not about building a SharePoint 2010 development environment.  It’s about one piece of that puzzle, specifically a simple pattern to automate the executing, restarting, and continuation of a PowerShell script.

So when I started I had one goal.  I wanted to be able to take a fresh installation of Windows Server 2008 R2 and with one click install and configure all of my development tools.  At the time I knew next to nothing about PowerShell but I was pretty sure it was going to be the correct tool.

The first hurdle I had to get over, and the one this article focuses on, was how to get my PowerShell script to restart the box and then continue where it left off.  Some quick searching got me headed in the correct direction.  Basically you can get PowerShell to write to the registry and by doing so you can get it to add itself to the Windows startup routine.  With some clever passing of parameters you have the ability to get the script to start where it left off.  Here’s a stripped down version of the code:

param($global:Step=1)

function Set-NextStep() {

  $global:Step++

  Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" -Name "SystemSetupStep" -Value "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe &C:\Setup.ps1 -Step $global:Step"

}

function Run-NextStep() {

  switch ($global:Step) {

    1 { Step-1; break; }

    2 { Step-2; break; }

    default { Remove-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" -Name "SystemSetupStep" }

  }

}

function Step-1() {

  Write-Host "This is step 1"

  Set-NextStep

  Restart-Computer

}

function Step-2() {

  Write-Host "This is step 2"

  Set-NextStep

  Run-NextStep

}

Run-NextStep

 

Ok, so let’s break this thing down.  First line is the $Step parameter.  This allows me to pass a value into the script when I call it from the command line or if I omit the parameter it sets itself to 1.

Next is the function Set-NextStep().  It increments the parameter $Step by 1 and then writes some information to the registry.  Specifically it adds an entry that tells Windows to execute a command on startup.  What is that command?  I’m asking it to start the PowerShell command window and to load my script as well as pass in the parameter $Step which would now be one value greater than when I started.

The Run-NextStep() function is simply a switch statement driven by the value of the parameter $Step.  What I really like about this is how well it documents what is actually going on.  It clearly shows you what steps are going to be executed and in what order.  The default behavior of the switch statement is some cleanup of the registry to remove the command that tells Windows to execute the script on startup.

Then I have two sample functions in there.  Step-1() outputs to the command window and then sets the next step before it restarts the computer.  Step-2 outputs to the command window sets the next step and runs the next step.

And that is that.  A simple little pattern to execute multiple functions in PowerShell over as many reboots as you need.  In the future maybe I’ll post my full blow version of this that actually does build a SharePoint 2010 Development box.

 

 

Posted: Mar 07 2012, 04:28 by Michael.Thompson | Comments (0) RSS comment feed

Tags: ,
Categories: General | Portals & Collaboration

Tags

Categories

Archive

Blogroll

Neudesic Social Media

Follow Neudesic on Twitter Follow Neudesic on Facebook Neudesic Neudesic