File size: 3,387 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Management.Automation;

using Dbg = System.Management.Automation;

namespace Microsoft.PowerShell.Commands
{
    /// <summary>
    /// A command that begins a transaction.
    /// </summary>
    [Cmdlet(VerbsLifecycle.Start, "Transaction", SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=135262")]
    public class StartTransactionCommand : PSCmdlet
    {
        /// <summary>
        /// The time, in minutes, before this transaction is rolled back
        /// automatically.
        /// </summary>
        [Parameter]
        [Alias("TimeoutMins")]
        public int Timeout
        {
            get
            {
                return (int)_timeout.TotalMinutes;
            }

            set
            {
                // The transactions constructor treats a timeout of
                // zero as infinite. So we fudge it to be a bit longer.
                if (value == 0)
                    _timeout = TimeSpan.FromTicks(1);
                else
                    _timeout = TimeSpan.FromMinutes(value);

                _timeoutSpecified = true;
            }
        }

        private bool _timeoutSpecified = false;
        private TimeSpan _timeout = TimeSpan.MinValue;

        /// <summary>
        /// Gets or sets the flag to determine if this transaction can
        /// be committed or rolled back independently of other transactions.
        /// </summary>
        [Parameter]
        public SwitchParameter Independent
        {
            get { return _independent; }

            set { _independent = value; }
        }

        private SwitchParameter _independent;

        /// <summary>
        /// Gets or sets the rollback preference for this transaction.
        /// </summary>
        [Parameter]
        public RollbackSeverity RollbackPreference
        {
            get { return _rollbackPreference; }

            set { _rollbackPreference = value; }
        }

        private RollbackSeverity _rollbackPreference = RollbackSeverity.Error;

        /// <summary>
        /// Creates a new transaction.
        /// </summary>
        protected override void EndProcessing()
        {
            if (ShouldProcess(
                NavigationResources.TransactionResource,
                NavigationResources.CreateAction))
            {
                // Set the default timeout
                if (!_timeoutSpecified)
                {
                    // See if we're being invoked directly at the
                    // command line. In that case, set the timeout to infinite.
                    if (MyInvocation.CommandOrigin == CommandOrigin.Runspace)
                    {
                        _timeout = TimeSpan.MaxValue;
                    }
                    else
                    {
                        _timeout = TimeSpan.FromMinutes(30);
                    }
                }

                // Create the new transaction
                if (_independent)
                {
                    this.Context.TransactionManager.CreateNew(_rollbackPreference, _timeout);
                }
                else
                {
                    this.Context.TransactionManager.CreateOrJoin(_rollbackPreference, _timeout);
                }
            }
        }
    }
}