Transaction Support in PowerShell V2

PowerShell V2 is the first script environment worldwide with transaction support.

At TechEd EMEA in Barcelona, PowerShell Architect Jeffrey Snover publicly demonstrated all the new V2 features - transactions are just one of many

So, what exactly are transactions, and what are they for?

Generally, when you execute scripts, three things can happen:

  • The script runs successfully, you are happy.
  • The script fails to run, you may not be quite as happy but generally you are still fine because you know where you are.
  • The script runs only partially and then dies unexpectedly in the middle of things. This is just about as bad as an ATM machine successfully debiting your checking account but then failing before spitting out your cash. Things get out of balance and control, and people can get hurt. Not good.

Simply put: Transactions eliminate this last variant and make scripts always either run or fail.

To do this, they "bundle" critical script commands and make them a singular transaction which can only either succeed or fail - but nothing inbetween. Another way of looking at this is from the perspective of a sandbox: all you do only happens in an isolated "sandbox" or "staging area" which is not visible to the system. Once the transaction is committed, only then will the changes be propagated to the live system. If something goes wrong before your transaction is done, the staging area is simply discarded.

Working with Transactions

So here is how you manage transactions with PowerShell V2. Transactions are a capability of providers. For now, the Registry provider is the first provider to support transactions, and you can check that out with Get-PSProviders and you will see in the Capability column a new capability called "Transaction".

To start a transaction and create a "sandbox", use Start-Transaction. But watch out, this does not turn on your airbag yet: All commands are still executed against your live system unless you use the -UseTransaction switch parameter.

Whenever you use this switch, you work against the sandbox. Without it, you see the "real world". Or to put it differently: Only those Cmdlets that support the -UseTransaction switch can participate in transactions, and only when you specify this switch will the command be part of your transaction.

To commit the results of your transaction to the live system, call Complete-Transaction. If something goes wrong and you'd like to "undo" your changes, simply call Undo-Transaction instead. The first Cmdlet will propagate the aggregated results in your sandbox to your life system, the latter will simply delete the sandbox with all the changes you made within.

To sum it up: you can wrap critical code in Start-Transaction / Complete-Transaction blocks and safely bail out on error with Undo-Transaction. While in such a transaction block, you need to use the -UseTransaction switch for any command you want to be sandboxed.

Let the PowerShell be with you!

-Tobias

PowerShellPlus


Posted Nov 08 2008, 02:40 PM by Tobias Weltner

Comments

Dreaming in PowerShell wrote PowerShell V2 Feature Overview
on 11-09-2008 4:43 AM

In Barcelona, PowerShell architect Jeffrey Snover disclosed some details about the next PowerShell generation

Concentrated Tech NSoftware Dell Compellent Sponsored by Idera and Concentrated Tech and NSoftware and Dell Compellent
Copyright 2011 PowerShell.com. All rights reserved.