Exercise 3: PowerShell Transactions
In this lab you will explore how transactions work with PowerShell by deleting and modifying registry values as part of a transaction. Doing so will allow us to discard any changes in case we find out that the end result was not as expected. You will then carry out other modifications and will commit the operations as part of the transaction.
Please note that this exercise can be carried out an a server core machine running the beta version of Windows Server 2008 R2 as it comes bundled with PowerShell v2 CTP, which supports registry transactions.
Part 1: Understanding Transactions
The following exercise must be carried out on your server core machine.
This exercise assumes that you already have PowerShell installed on your server core machine. If you do not have it installed, you can quickly install this feature by typing the following command:
dism /online /enable-feature /featurename:MicrosoftWindowsPowerShell
- On your server machine, start PowerShell from the command prompt by typing:
C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe
Even though the folder is called v1.0, the version of PowerShell you are using is v2.0 (CTP2)
- You should now have a prompt that displays:
PS C:\>
- Open Regedit by typing in the command prompt:
Regedit
- Using regedit, navigate the following registry key:
HKEY_CURRENT_USER
Throughout this exercise, you will be creating registry keys from PowerShell. RegEdit will be used to verify whether the changes took place or not.
- Switch back to the PowerShell command prompt and push the registry path we just manually visited into PowerShell’s default stack by typing:
pushd hkcu:\
hkcu is the abbreviation for HKEY_CURRENT_USER
- Type dir to verify that you are indeed at the root lever of HKEY_CURRENT_USER:
dir
The contents should be the same as the registry node you inspected when you opened regedit.
- Let’s create a new node under HKCU by typing:
New-Item –Name Test –ItemType Container –Force
Type dir once more to see the newly created item:
Feel free to switch back and compare the new results with RegEdit at any time but remember that you may need to refresh the contents to see the changes.
- Set the location to the Key that we just created:
Set-Location test
- In order to find out of the version of PowerShell we are using supports transactions, type the following:
Get-PSProvider Registry
Under the capabilities column, you can see that “Transactions” is listed. The registry is one of the components that PowerShell will support with transactions in v2.0. What does this mean? Let’s find out!
PowerShell supports various operations with transactions that were previously covered. In order to remember what these are, the following command will show you this information:
get-command –type cmdlet *Transaction
- Let’s try and make some changes to the registry with a transaction. Every transaction must begin with the Start-Transaction command:
Start-Transaction
As the warning specifies, the only commands that will be part of the transaction are those that have the Use-Transaction flag.
- Let’s create two keys under our test key. Each of these keys will have the exact content (recursively) of the Console Key under HKCU:
Copy-Item HKCU:\Console .\Copy1 -Recurse -Verbose -UseTransaction Copy-Item HKCU:\Console .\Copy2 -Recurse -Verbose -UseTransaction
Run a Get-ChildItem command to verify whether the keys were created:
Get-ChildItem
As you can see, the keys are nowhere to be found in the current registry key. This is because the commands were issued with the UseTransaction flag and will not happen until the current transaction is committed.
In order to find out the effects that something would have if a transaction was committed; you can issue the Use-Transaction flag. This will not commit the transaction but rather show you a preview of what the changes would be:
Get-ChildItem –UseTransaction
See the difference in the output now that you included the UseTransaction flag?
- Since the changes look like something we want for this exercise, let’s commit the operation issuing:
Complete-Transaction
- Run a Get-ChildItem command to verify whether the keys were created:
Get-ChildItem .
Since the transaction was committed, our commands show its effects in the registry.
- Let’s try and something more drastic, but before let’s make sure we make this a part of a transaction:
Start-Transaction
- Change directory up one level to be able to delete the current key:
cd ..
- Let’s do something drastic:
Remove-item HKCU:\Test\* -Recurse -Force -Verbose –UseTransaction
Don’t forget to include the UseTransaction flag or you won’t be able to roll back changes later on.
- Luckily for us, we are using a transaction and we can see the effect of such operation:
Get-ChildItem .\Test
Are the keys there? How come? Did we forget to add something to the line above?
- In order to undo the change from the transaction, all we have to do is issue the following command:
Undo-Transaction
Let’s see if the change is there:
Get-ChildItem .\Test
As expected, none of the changes happened because we opted out of committing the transaction.
Try to carry out the following operation on your own:
Please wait for the Instructor to resume the lecture.
Exercise-3: Conclusions
Transactions have proven to be a very effective way to deal with operations and flows that have an all-or-nothing result. By incorporating transactions into PowerShell, you will be in full control whenever you want to make sure that all the steps in your script flow entirely execute. In case that these operations do not execute successfully, with transactions you have an effective way to return the system to the state it was prior to script execution.
Remember that as of CTP3, the registry provider is the only provider that currently supports transactions, but you can expect this to change very soon.