| description | How to Support Transactions |
|---|---|
| ms.date | 09/13/2016 |
| title | How to Support Transactions |
This example shows the basic code elements that add support for transactions to a cmdlet.
Important
For more information about how Windows PowerShell handles transactions, see About Transactions.
-
When you declare the Cmdlet attribute, specify that the cmdlet supports transactions. When the cmdlet supports transactions, Windows PowerShell adds the
UseTransactionparameter to the cmdlet when it is run.[Cmdlet(VerbsCommunications.Send, "GreetingTx", SupportsTransactions=true )]
-
Within one of the input processing methods, add an
ifblock to determine if a transaction is available. If theifstatement resolves totrue, the actions within this statement can be performed within the context of the current transaction.if (TransactionAvailable()) { using (CurrentPSTransaction) { WriteObject("Hello " + name + " from within a transaction."); } }