Exercise 3: PowerShell Transactions

Figure 1

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

Figure 2

Note:
The following exercise must be carried out on your server core machine.

Note:
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:

PowerShell

dism /online /enable-feature /featurename:MicrosoftWindowsPowerShell
  1. On your server machine, start PowerShell from the command prompt by typing:

    C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe
    Note:
    Even though the folder is called v1.0, the version of PowerShell you are using is v2.0 (CTP2)
  2. You should now have a prompt that displays:

    PS C:\>
  3. Open Regedit by typing in the command prompt:

    PowerShell

    Regedit
  4. Using regedit, navigate the following registry key:

    HKEY_CURRENT_USER
    Note:
    Throughout this exercise, you will be creating registry keys from PowerShell. RegEdit will be used to verify whether the changes took place or not.
  5. Switch back to the PowerShell command prompt and push the registry path we just manually visited into PowerShell’s default stack by typing:

    PowerShell

    pushd hkcu:\
    Note:
    hkcu is the abbreviation for HKEY_CURRENT_USER
  6. Type dir to verify that you are indeed at the root lever of HKEY_CURRENT_USER:

    PowerShell

    dir
    Note:
    The contents should be the same as the registry node you inspected when you opened regedit.
  7. Let’s create a new node under HKCU by typing:

    PowerShell

    New-Item –Name Test –ItemType Container –Force
  8. Type dir once more to see the newly created item:

    Figure 3

    Note:
    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.
  9. Set the location to the Key that we just created:

    PowerShell

    Set-Location test
  10. In order to find out of the version of PowerShell we are using supports transactions, type the following:

    PowerShell

    Get-PSProvider Registry
    Note:
    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!
  11. 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:

    PowerShell

    get-command –type cmdlet *Transaction

    Figure 4

  12. Let’s try and make some changes to the registry with a transaction. Every transaction must begin with the Start-Transaction command:

    PowerShell

    Start-Transaction
    Note:
    As the warning specifies, the only commands that will be part of the transaction are those that have the Use-Transaction flag.
  13. 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:

    PowerShell

    Copy-Item HKCU:\Console .\Copy1 -Recurse -Verbose -UseTransaction Copy-Item HKCU:\Console .\Copy2 -Recurse -Verbose -UseTransaction
  14. Run a Get-ChildItem command to verify whether the keys were created:

    Get-ChildItem
    Note:
    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.

    Figure 5

  15. 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:

    PowerShell

    Get-ChildItem –UseTransaction
    Note:
    See the difference in the output now that you included the UseTransaction flag?

    Figure 6

  16. Since the changes look like something we want for this exercise, let’s commit the operation issuing:

    PowerShell

    Complete-Transaction
  17. Run a Get-ChildItem command to verify whether the keys were created:

    PowerShell

    Get-ChildItem .
    Note:
    Since the transaction was committed, our commands show its effects in the registry.
  18. Let’s try and something more drastic, but before let’s make sure we make this a part of a transaction:

    PowerShell

    Start-Transaction
  19. Change directory up one level to be able to delete the current key:

    PowerShell

    cd ..
  20. Let’s do something drastic:

    PowerShell

    Remove-item HKCU:\Test\* -Recurse -Force -Verbose –UseTransaction
    Note:
    Don’t forget to include the UseTransaction flag or you won’t be able to roll back changes later on.
  21. Luckily for us, we are using a transaction and we can see the effect of such operation:

    PowerShell

    Get-ChildItem .\Test
    Note:
    Are the keys there? How come? Did we forget to add something to the line above?
  22. In order to undo the change from the transaction, all we have to do is issue the following command:

    PowerShell

    Undo-Transaction
  23. Let’s see if the change is there:

    PowerShell

    Get-ChildItem .\Test
    Note:
    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:

    • Delete the Copy2 registry key using transactions to make sure you delete the intended one.

    Note:
    Please wait for the Instructor to resume the lecture.

Exercise-3: Conclusions

Figure 7

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.