How-to: Perform a set of actions in a single Transaction.

Manage transacted operations
This feature enables you to start a transaction, indicate which commands are part of the transaction and then either commit or roll back (cancel) the transaction.

Commands in a transaction are managed as a unit, either all commands are committed, or all commands are rolled back. To participate in a transaction, both the cmdlet and the provider must support transactions. In some cases, (e.g. the windows registry) transaction support requires Vista or above.

The -UseTransaction parameter (or its alias -usetx)
Cmdlets that can support transactions have a -UseTransaction parameter. This parameter can be used only when the session contains an active transaction and includes the command in the active transaction.

The Transactions Object

Transactions are represented in Windows PowerShell by System.Management.Automation.Transaction

The object has the following 3 properties:

RollbackPreference: Error, TerminatingError, or Never
Set the rollback preference with Start-Transaction.

Status: Active, Committed, and RolledBack
The current status of the transaction.

SubscriberCount: the number of subscribers to the transaction.

Multiple transactions can be in progress in the same session at the same
time, (nested) but only the most-recently started transaction is active.

Independent Transactions

If you start a transaction while another transaction is in progress, PowerShell will (by default) add a "subscriber" to the current transaction rather than start a new transaction.

When a transaction has multiple subscribers, a single Undo-Transaction command will roll back the entire transaction for all subscribers. However, you must still enter a Complete-Transaction command for every subscriber.

You can start a transaction that is independent of the current transaction by using the -Independent parameter of Start-Transaction.

When an independent transaction is finished (committed or rolled back), the original transaction becomes the active transaction again.

Locking

Unlike a database transaction, PowerShell transactions do not 'lock' any data, so other applications or other users could potentially change the data while a PowerShell transaction is running.

Examples

Add a new registry key:

Start-Transaction
cd hkcu:\Software
New-Item SS64 -UseTransaction
dir S* -useTransaction
Complete-Transaction

Start adding a new registry key, but then cancel the tx:

Start-Transaction
cd hkcu:\Software
New-Item SampleKey -UseTransaction
dir S* -useTransaction
Undo-Transaction

Start a transaction setting the preference to not rollback if an error occurs:

start-transaction -rollbackpreference Never

Evaluate an expression inside a tx:

use-transaction {$myVariable.append("some more text")} -usetx

Get the value of the SubscriberCount property of the active transaction:

(Get-Transaction).SubscriberCount

“It isn’t what you do, but how you do it” ~ John Wooden

Related PowerShell Cmdlets

Start-Transaction - Start a new transaction.
Complete-Transaction
- Commit the transaction.
Get-Transaction - Get information about the active transaction.
Use-Transaction
- Add a command or expression to the transaction.
Undo-Transaction
- Roll back a transaction.


 
Copyright © 1999-2024 SS64.com
Some rights reserved