Add a script block to the active transaction.
Syntax Use-Transaction [-TransactedScript] scriptblock [-UseTransaction] [CommonParameters] Key -TransactedScript scriptblock The script block that is run in the transaction. Enter any valid script block enclosed in braces { } This parameter is required. -UseTransaction Include the command in the active transaction. CommonParameters: -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable, -OutBuffer -OutVariable.
Examples
Show the effect of rolling back a transaction that includes Use-Transaction commands. When the transaction is rolled back, the transacted changes are discarded and the data is unchanged:
PS C:\> Start-transaction
PS C:\> $myTxString = New-Object Microsoft.PowerShell.Commands.Management.TransactedString
PS C:\> $myTxString.Append("Hello")
PS C:\> use-transaction -TransactedScript { $myTxString.Append(", World") } -UseTransaction
PS C:\> Undo-transaction
PS C:\> $myTxString.ToString()
Hello
“Two roads diverged in a wood, and I, I took the one less traveled by, And that has made all the difference” ~ Robert Frost (The Road Not Taken)
Related PowerShell Cmdlets:
Start-Transaction - Start a new transaction.
Complete-Transaction - Commit the transaction.
Get-Transaction - Get information about the active transaction.
Undo-Transaction - Roll back a transaction.