Powershell – Keeping record of where the script left off

What happens when your script is interrupted!

There are lots of ways to handle Powershell script execution that goes wrong! Today I want to tell you about one way of solving this problem. You start your script and it is doing all the magic that you told it to do. Then out of no where, the power goes out or the computer shuts off! Once everything is restored you are now sitting there wondering…where did it stop? Should I restart it from the beginning? What if my code runs again? Would it do harm or will it be okay?

Well, here is one way to solve this problem. I started using this for a computer build script. The script became several thousands of lines of code. Starting it over may not have harmed the computer but now I would have to wait for it to repeat everything!!

Let’s Begin using Powershell

Setup phase

We are going to create a new Powershell object using the New-Object cmdlet. We are then going to export the properties and their values to an XML file. The path to this XML file will go into $xml

$xml = 'PATH TO XML'

We need a way to create all of the properties and set there value to nothing. We are going to do this by creating a hashtable in the $properties variable. This is like an array but instead of just a list of values it is a list of key\value pairs. Since I was building computers, each property was named after the step that was being executed. The adobe property was for the code that installs Adobe Reader and so on.

$properties = @{
adobe='';
reader='';
java='';
sysrestore='';
power='';
choco='';
googlechrome='';
}

You will first need to use an IF statement and test for the existance of the XML file. If the test is true then you will want to import it. We will do this using the Import-Clixml cmdlet. If the test is false then we will create a new PSOBJECT using New-Object cmdlet. After we create the object we will immediately export it to the XML file using Export-Clixml cmdlet.

if (test-path -path $xml){
    $build = Import-Clixml $xml
} else {
    $build = NEW-OBJECT PSOBJECT -property $properties
    $build | Export-Clixml $xml
} 

Execution phase

Now that we have our object we can put it to use. We can us an IF statement to check the value of the property of our object. If the value is complete, we will do nothing. If the value is nothing, we will run our code to do whatever it is that we want to do. We can then check the results to ensure that everything worked. If everything was a success then we can change the value of the property to complete and export it to XML.

if ($build.adobe -eq 'complete'){
} else {
    # do some code
    # check to see if code worked
    $build.adobe = 'complete'
    $build | Export-Clixml $xml
}

if ($build.reader -eq 'complete'){
} else {
    # do some code
    # check to see if code worked
    $build.reader = 'complete'
    $build | Export-Clixml $xml
}


If the script were to stop at any point during this execution, the next time it was executed it would skip all steps that have been validated as complete. This would allow you to not waste time running the same steps over and over.

Here is the full code, enjoy!!!….

$xml = 'PATH TO XML'

$properties = @{
adobe='';
reader='';
java='';
sysrestore='';
power='';
choco='';
googlechrome='';
}

if (test-path -path $buildxml){
    $build = Import-Clixml $xml
} else {
    $build = NEW-OBJECT PSOBJECT -property $properties
    $build | Export-Clixml $xml
}  

if ($build.adobe -eq 'complete'){
} else {
    # do some code
    # check to see if code worked
    $build.adobe = 'complete'
    $build | Export-Clixml $xml
}

if ($build.reader -eq 'complete'){
} else {
    # do some code
    # check to see if code worked
    $build.reader = 'complete'
    $build | Export-Clixml $xml
}

Leave a Reply

Your email address will not be published.