Prof. Powershell

Transaction Figures

Call up transactions with PowerShell 2.0, but do so in a way that keeps you from popping up errors unexpectedly. The secret is in exceptions.

Often in administrative scripting, you may have automated a complex or multi-step process. Often all of the steps must be completed successfully in order for the entire process to be considered a success. One solution to this challenge is to use transactions.

A transaction is simply something you want to do. Once all you've run through all the steps, you complete the transaction. Naturally, you need to start the transaction before you run through your steps.

PowerShell 2.0 provides transaction support. Unfortunately, it isn't everywhere you would hope it would be. In order to use transactions, both the cmdlets you want to use and the PSProvider must support transactions. To see what providers support transactions run Get-PSProvider at a PowerShell prompt.

PS C:\> Get-PSProvider

More than likely you'll only see the Registry provider. Eventually more providers will add this capability, but once you understand the process you'll be ready to go.

The other part of the transaction challenge is that the cmdlet must have the UseTransaction parameter. Here's a short set of commands that demonstrates the process. You could put these lines in a PowerShell script. The first step is to begin the transaction process by calling the Start-Transaction cmdlet:

Start-Transaction
Write-Host "Creating some registry settings"

New-Item HKCU:ProfPowerShell -UseTransaction
New-ItemProperty -path HKCU:ProfPowerShell -Name Created -value (Get-Date) -UseTransaction
New-ItemProperty -path HKCU:ProfPowerShell -Name User -value $env:username -UseTransaction
New-ItemProperty -path HKCU:ProfPowerShell -Name Expires -value (Get-Date).AddDays(365) -UseTransaction
New-ItemProperty -path HKCU:ProfPowerShell -Name Comment -value "Practice PowerShell Every Day" -UseTransaction

write-host "Here is the transaction object"
Get-Transaction

#the following command will fail because you haven't completed the transaction
Get-ItemProperty HKCU:ProfPowerShell

Complete-Transaction

The script attempts to create a registry key and values under HKCU. Notice that all the New-Item and New-ItemProperty cmdlets are using the UseTransaction parameter. This tells PowerShell to keep and make the change, but don't really commit just yet. We can verify this because when we try to look at the new item before we complete the transaction, we get an error. It doesn't exist. Yet if you look after completing the transaction, it will be found. The solution is to complete the transaction before you attempt anything else:

Get-Transaction
Complete-Transaction
Get-ItemProperty HKCU:ProfPowerShell

But what if something goes wrong? That's the whole point right? Let's go through this again, but this time introduce an exception:

Start-Transaction
Write-Host "Creating some registry settings"

New-Item HKCU:ProfPowerShell -UseTransaction
New-ItemProperty -path HKCU:ProfPowerShell -Name Created -value (Get-Date) -UseTransaction
New-ItemProperty -path HKCU:ProfPowerShell -Name User -value $env:username -UseTransaction
#this command should throw an exception
New-ItemProperty -path HKCU:ProfPowerShell -Name Expires -value (Get-Date).AddDays(365) -PropertyType FOO -UseTransaction

The last command will force PowerShell to terminate and throw an exception. Because the default behavior of a transaction object is to rollback on an error, all the previous steps are undone. In fact, if you continue with the rest of the code you'll get errors and see that the transaction object has been rolled back.

I've just touched the surface on this topic. Be sure to take a few minutes to read the About_Transactions help file.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular