取得目前的 (現用) 交易。

語法

Get-Transaction [<CommonParameters>]

描述

Get-Transaction Cmdlet 取得表示工作階段中目前交易的物件。

因為每次只會有一個現用的交易,因此這個 Cmdlet 永遠只會傳回一個物件。如果您啟動一個或多個獨立交易 (透過使用 Start-Transaction 的 Independent 參數的方式),最近所啟動的交易會處於現用狀態,且為 Get-Transaction 傳回的交易。

當所有現用交易都已復原或認可時,Get-Transaction 會顯示工作階段中最近一次處理現用狀態的交易。

在支援 Windows PowerShell 交易功能的一組 Cmdlet 集中,Get-Transaction Cmdlet 是其中一項。如需詳細資訊,請參閱 about_Transactions。

參數

<CommonParameters>

這個 Cmdlet 支援一般參數:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。如需詳細資訊,請參閱 about_Commonparameters.

輸入和輸出

輸入型別是可經由管道輸出至 Cmdlet 的物件型別。傳回型別則是 Cmdlet 所傳回的物件型別。

輸入

您無法經由管道將物件輸出至這個 Cmdlet。

輸出

System.Management.Automation.PSTransaction

Get-Transaction 會傳回代表目前交易的物件。

範例 1

C:\PS>start-transaction

C:\PS> get-transaction 

RollbackPreference   SubscriberCount   Status
------------------   ---------------   ------
Error                1                 Active

描述
-----------
這個命令使用 Get-Transaction Cmdlet 取得目前的交易。






範例 2

C:\PS>get-transaction | get-member

Name               MemberType Definition
----               ---------- ----------
Dispose            Method     System.Void Dispose(), System.Void Dispose(Boolean disposing)
Equals             Method     System.Boolean Equals(Object obj)
GetHashCode        Method     System.Int32 GetHashCode()
GetType            Method     System.Type GetType()
ToString           Method     System.String ToString()
IsCommitted        Property   System.Boolean IsCommitted {get;}
IsRolledBack       Property   System.Boolean IsRolledBack {get;}
RollbackPreference Property   System.Management.Automation.RollbackSeverity RollbackPreference {get;}
SubscriberCount    Property   System.Int32 SubscriberCount {get;set;}

描述
-----------
這個命令使用 Get-Member Cmdlet 顯示交易物件的屬性與方法。






範例 3

C:\PS>cd hklm:\software
HKLM:\SOFTWARE> Start-Transaction
HKLM:\SOFTWARE> New-Item MyCompany -UseTransaction
HKLM:\SOFTWARE> Undo-Transaction
HKLM:\SOFTWARE> Get-Transaction

RollbackPreference   SubscriberCount   Status
------------------   ---------------   ----------
Error                0                 RolledBack

描述
-----------
這個命令顯示已復原之交易的交易物件屬性值。






範例 4

C:\PS>cd hklm:\software
HKLM:\SOFTWARE> Start-Transaction
HKLM:\SOFTWARE> New-Item MyCompany -UseTransaction
HKLM:\SOFTWARE> Complete-Transaction
HKLM:\SOFTWARE> Get-Transaction

RollbackPreference   SubscriberCount   Status
------------------   ---------------   ---------
Error                1                 Committed

描述
-----------
這個命令顯示已認可之交易的交易物件屬性值。






範例 5

C:\PS>cd hklm:\software
HKLM:\SOFTWARE> Start-Transaction
HKLM:\SOFTWARE> New-Item MyCompany -UseTransaction

HKLM:\SOFTWARE> Start-Transaction
HKLM:\SOFTWARE> New-Item MyCompany2 -UseTransaction

HKLM:\SOFTWARE> Get-Transaction

RollbackPreference   SubscriberCount   Status
------------------   ---------------   ------
Error                2                 Active

HKLM:\SOFTWARE> Complete-Transaction
HKLM:\SOFTWARE> Get-Transaction

RollbackPreference   SubscriberCount   Status
------------------   ---------------   ------
Error                1                 Active


HKLM:\SOFTWARE> Complete-Transaction
HKLM:\SOFTWARE> Get-Transaction

RollbackPreference   SubscriberCount   Status
------------------   ---------------   ---------
Error                1                 Committed

描述
-----------
這個範例顯示在其他交易進行時啟動交易時,對交易物件產生的作用。通常,當執行交易的指令碼包含函數或是呼叫包含另一個完整交易的指令碼時,才會發生這種情況。

除非第二個 Start-Transaction 命令包括 Independent 參數,否則 Start-Transaction 不會建立新交易,而會將第二個訂閱者新增的原始交易。

第一個 Start-Transaction 命令會啟動交易。使用 UseTransaction 參數的 New-Item 命令屬於交易的一部分。

第二個 Start-Transaction 命令將訂閱者加入交易。下一個 New-Item 命令也是交易的一部分。

第一個 Get-Transaction 命令顯示多個訂閱者的交易。請注意,訂閱者計數為 2。

第一個 Complete-Transaction 命令不會認可交易,但是會將訂閱者計數降低到 1。

第二個 Complete-Transaction 命令認可交易。






範例 6

C:\PS>HKLM:\SOFTWARE> Start-Transaction

HKLM:\SOFTWARE> Get-Transaction

RollbackPreference   SubscriberCount   IsRolledBack   IsCommitted
------------------   ---------------   ------------   -----------
Error                1                 False          False

HKLM:\SOFTWARE> Start-Transaction -Independent
HKLM:\SOFTWARE> Get-Transaction

RollbackPreference   SubscriberCount   IsRolledBack   IsCommitted
------------------   ---------------   ------------   -----------
Error                1                 False          False

HKLM:\SOFTWARE> Complete-Transaction
HKLM:\SOFTWARE> Get-Transaction

HKLM:\SOFTWARE> Complete-Transaction
HKLM:\SOFTWARE> Get-Transaction

描述
-----------
這個範例顯示在其他交易進行時啟動獨立交易時,對交易物件產生的作用。

第一個 Start-Transaction 命令會啟動交易。使用 UseTransaction 參數的 New-Item 命令屬於交易的一部分。

第二個 Start-Transaction 命令將訂閱者加入交易。下一個 New-Item 命令也是交易的一部分。

第一個 Get-Transaction 命令顯示多個訂閱者的交易。請注意,訂閱者計數為 2。

Complete-Transaction 命令會將訂閱者計數降低到 1,但是不會認可交易。

第二個 Complete-Transaction 命令認可交易。






請參閱




目錄