Share via


Exerise 3: PowerShell Transactions (Background)

Figure 1

Transactions have long been a staple of the database world. With a transaction, you carry out a number of operations (e.g., changes to the database) without actually changing the database in any way. Why would you want to do that? Well, you’d want to do that if your end goal was all-or-nothing: you either want every single operation carried out (without errors) or you don’t want any of the operations carried out.

Take this scenario, for example. Suppose your company has decided to give everyone a 10 percent raise. (Hey, we just said it was a scenario; we didn’t say it was a realistic scenario.) Your accounting department might want to update the payroll database as a transaction. Why? Because that way they could ensure that each and every employee gets the 10 percent raise. If something goes wrong partway through the operation the accountants can simply roll back the transaction. Suppose you didn’t grant the raise as a transaction, and suppose that, midway through the process, something did go wrong. At that point you have a problem: now you have to figure out who got the raise and who didn’t. With a transaction, you can carry out the operation and verify that no errors occurred. If everything went fine then you can complete the transaction; if something went wrong, you can roll back the transaction, keeping the database in the same shape it was before you started, and giving you the opportunity to try again.

Now, before you get too excited we should note that PowerShell’s support for transactions is somewhat limited at the moment. The ability to carry out transactions must be built into the PowerShell provider, and as far as we know only the registry provider currently supports the use of transactions.