File size: 3,168 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Management.Automation;
using System.Management.Automation.Internal;
using Dbg = System.Management.Automation;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// A command that commits a transaction.
/// </summary>
[Cmdlet(VerbsOther.Use, "Transaction", SupportsTransactions = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=135271")]
public class UseTransactionCommand : PSCmdlet
{
/// <summary>
/// This parameter specifies the script block to run in the current
/// PowerShell transaction.
/// </summary>
[Parameter(Position = 0, Mandatory = true)]
public ScriptBlock TransactedScript
{
get
{
return _transactedScript;
}
set
{
_transactedScript = value;
}
}
private ScriptBlock _transactedScript;
/// <summary>
/// Commits the current transaction.
/// </summary>
protected override void EndProcessing()
{
using (CurrentPSTransaction)
{
try
{
var emptyArray = Array.Empty<object>();
_transactedScript.InvokeUsingCmdlet(
contextCmdlet: this,
useLocalScope: false,
errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe,
dollarUnder: null,
input: emptyArray,
scriptThis: AutomationNull.Value,
args: emptyArray);
}
catch (Exception e)
{
// Catch-all OK. This is a third-party call-out.
ErrorRecord errorRecord = new ErrorRecord(e, "TRANSACTED_SCRIPT_EXCEPTION", ErrorCategory.NotSpecified, null);
// The "transaction timed out" exception is
// exceedingly obtuse. We clarify things here.
bool isTimeoutException = false;
Exception tempException = e;
while (tempException != null)
{
if (tempException is System.TimeoutException)
{
isTimeoutException = true;
break;
}
tempException = tempException.InnerException;
}
if (isTimeoutException)
{
errorRecord = new ErrorRecord(
new InvalidOperationException(
TransactionResources.TransactionTimedOut),
"TRANSACTION_TIMEOUT",
ErrorCategory.InvalidOperation,
e);
}
WriteError(errorRecord);
}
}
}
}
}
|