cmdlet
Start-Transaction
启动一个事务,该事务是作为一个单元管理的一系列命令。 事务可以完成或提交。 或者,它可以完全撤消或回滚,以便事务更改的任何数据还原到其原始状态。
由于事务中的命令作为一个单元进行管理,因此所有命令不是全部提交就是全部回滚。
默认情况下,如果事务中的任何命令生成错误,则事务会自动回滚。 可以使用
RollbackPreference
参数更改此行为。
事务中使用的 cmdlet 必须设计为支持事务。 支持事务的 Cmdlet 具有
UseTransaction
参数。 若要通过提供程序执行事务,则该提供程序必须支持事务。 Windows Vista 和更高版本的 Windows 操作系统中的 Windows PowerShell 注册表提供程序支持事务。 还可以使用
Microsoft.PowerShell.Commands.Management.TransactedString
类在支持Windows PowerShell的任何 Windows 系统上的事务中包含表达式。 其他 Windows PowerShell 提供程序也可以支持事务。
一次只能有一个事务处于活动状态。 如果在事务正在进行时启动新的独立事务,则新事务将成为活动事务,并且必须在对原始事务进行任何更改之前提交或回滚新事务。
Start-Transaction
cmdlet 是支持 Windows PowerShell 中的事务功能的一组 cmdlet 之一。 有关详细信息,请参阅
about_Transactions
。
示例 1:启动和回滚事务
Set-Location hkcu:\software
Start-Transaction
New-Item "ContosoCompany" -UseTransaction
New-ItemProperty "ContosoCompany" -Name "MyKey" -Value 123 -UseTransaction
Undo-Transaction
这些命令先启动事务,然后回滚事务。 由于回滚了事务,因此未对注册表进行更改。
示例 2:启动并完成事务
Set-Location hkcu:\software
Start-Transaction
New-Item "ContosoCompany" -UseTransaction
New-ItemProperty "ContosoCompany" -Name "MyKey" -Value 123 -UseTransaction
Complete-Transaction
这些命令先启动事务,然后完成事务。 在使用 命令之前
Complete-Transaction
,不会对注册表进行更改。
示例 3:使用不同的回滚首选项
Set-Location HKCU:\software
Start-Transaction
New-Item -Path "NoPath" -Name "ContosoCompany" -UseTransaction
New-Item -Path . -Name "ContosoCompany" -UseTransaction
Start-Transaction -RollbackPreference never
New-Item -Path "NoPath" -Name "ContosoCompany" -UseTransaction
New-Item -Path . -Name "ContosoCompany" -UseTransaction
# Start-Transaction (-rollbackpreference error)
Start-Transaction
New-Item -Path "NoPath" -Name "ContosoCompany" -UseTransaction
New-Item : The registry key at the specified path does not exist.
At line:1 char:9
+ new-item <<<< -Path NoPath -Name ContosoCompany -UseTransaction
New-Item -Path . -Name "Contoso" -UseTransaction
New-Item : Cannot use transaction. The transaction has been rolled back or has timed out.
At line:1 char:9
+ New-Item <<<< -Path . -Name ContosoCompany -UseTransaction
# Start-Transaction (-rollbackpreference never)
Start-Transaction -RollbackPreference never
New-Item -Path "NoPath" -Name "ContosoCompany" -UseTransaction
New-Item : The registry key at the specified path does not exist.
At line:1 char:9
+ New-Item <<<< -Path NoPath -Name "ContosoCompany" -UseTransaction
New-Item -Path . -Name "ContosoCompany" -UseTransaction
Hive: HKEY_CURRENT_USER\Software
SKC VC Name Property
--- -- ---- --------
0 0 ContosoCompany {}
Complete-Transaction
# Succeeds
此示例演示更改
RollbackPreference
参数值的效果。
在第一组命令中,
Start-Transaction
不使用
RollbackPreference
。 因此,使用默认值 (Error) 。 当事务命令中发生错误时,即指定的路径不存在时,会自动回滚该事务。
第二组命令
Start-Transaction
使用值为 Never 的
RollbackPreference
。 因此当事务命令发生错误时,事务仍处于活动状态并且可以成功完成。
由于大多数事务必须在不出错的情况下执行,因此通常首选
默认值 RollbackPreference
。
示例 4:在事务正在进行时使用此 cmdlet
Set-Location HKCU:\software
Start-Transaction
New-Item "ContosoCompany" -UseTransaction
Start-Transaction
Get-Transaction
New-Item "ContosoCompany2" -UseTransaction
Complete-Transaction
Complete-Transaction
Get-Transaction
RollbackPreference SubscriberCount Status
------------------ --------------- ------
Error 2 Active
此示例演示事务正在进行时 使用
Start-Transaction
的效果。
这与加入正在执行的事务的效果非常类似。
虽然这是一个简化的命令,但这种情况经常发生在事务涉及运行包含完整事务的脚本时。
第一个
Start-Transaction
命令启动事务。 第一个
New-Item
命令是事务的一部分。
第二
Start-Transaction
个命令将新的订阅服务器添加到事务。 命令
Get-Transaction
现在返回一个订阅者计数为 2 的事务。 第二
New-Item
个命令是同一事务的一部分。
在整个事务完成之前,不会对注册表进行更改。 若要完成事务,必须输入两
Complete-Transaction
个命令,每个订阅者一个。 如果在任何时候回滚事务,所有事务都将为两个订阅者回滚。
示例 5:在独立事务正在进行时启动独立事务
Set-Location HKCU:\software
Start-Transaction
New-Item "ContosoCompany" -UseTransaction
Start-Transaction -Independent
Get-Transaction
Undo-Transaction
New-ItemProperty -Path "ContosoCompany" -Name "MyKey" -Value 123 -UseTransaction
Complete-Transaction
Get-ChildItem contoso*
Get-Transaction
RollbackPreference SubscriberCount Status
------------------ --------------- ------
Error 1 Active
Undo-Transaction
New-ItemProperty -Path "ContosoCompany" -Name "MyKey" -Value 123 -UseTransaction
MyKey
-----
Complete-Transaction
Get-ChildItem contoso*
Hive: HKEY_CURRENT_USER\Software
SKC VC Name Property
--- -- ---- --------
0 1 MyCompany {MyKey}
此示例演示在另一个事务正在进行时使用
的
Start-Transaction
Independent 参数启动事务的效果。
在本例中,将回滚新事务,而不影响原来的事务。
虽然事务在逻辑上是独立的,但由于一次只能有一个事务处于活动状态,因此你必须回滚或提交最新事务,然后才能继续处理原来的事务。
第一组命令启动一个事务。
命令
New-Item
是第一个事务的一部分。
第二组命令使用
Start-Transaction
Independent
参数。
下面的
Get-Transaction
命令显示活动事务的事务对象,这是最新的事务。
订阅者计数等于 1,表示事务不相关。
使用
Undo-Transaction
命令回滚活动事务时,原始事务将再次变为活动状态。
命令
New-ItemProperty
是原始事务的一部分,完成时不会出错,并且可以使用 命令完成
Complete-Transaction
原始事务。
因此注册表会发生更改。
示例 6:运行不属于事务的命令
Set-Location hkcu:\software
Start-Transaction
New-Item "ContosoCompany1" -UseTransaction
New-Item "ContosoCompany2"
New-Item "ContosoCompany3" -UseTransaction
Get-ChildItem contoso*
Hive: HKEY_CURRENT_USER\Software
SKC VC Name Property
--- -- ---- --------
0 0 ContosoCompany2 {}
Complete-Transaction
Get-ChildItem contoso*
Hive: HKEY_CURRENT_USER\Software
SKC VC Name Property
--- -- ---- --------
0 0 ContosoCompany1 {}
0 0 ContosoCompany2 {}
0 0 ContosoCompany3 {}
此示例演示了在事务正在执行时提交的命令可以包含在事务中,也可以不包含在事务中。 只有使用
UseTransaction 参数的
命令是事务的一部分。
第一个和第三
New-Item
个命令使用
UseTransaction
参数。 这两个命令都包含在事务中。 由于第二个
New-Item
命令不使用
UseTransaction
参数,因此它不是事务的一部分。
第一个Get-ChildItem命令显示效果。 第二
New-Item
个命令会立即完成,但在提交事务之前,第一个和第三
New-Item
个命令无效。
命令
Complete-Transaction
提交事务。 因此,第二个Get-ChildItem命令显示所有新项都已添加到注册表中。
示例 7:回滚未在指定时间内完成的事务
Start-Transaction -Timeout 2
# Wait two minutes...
Get-Transaction
New-Item HKCU:\Software\ContosoCompany -UseTransaction
Start-Transaction -Timeout 2
# Wait two minutes...
Get-Transaction
RollbackPreference SubscriberCount Status
------------------ --------------- -----------
Error 1 RolledBack
New-Item HKCU:\Software\ContosoCompany -UseTransaction
New-Item : Cannot use transaction. The transaction has been rolled back or has timed out.
At line:1 char:9
+ new-item <<<< MyCompany -UseTransaction
此命令使用 的
Start-Transaction
Timeout
参数启动必须在两分钟内完成的事务。 如果事务在超时到期时未完成,则会自动回滚。
超时到期后,不会通知你,但事务对象的
Status
属性设置为 RolledBack,并且使用
UseTransaction
参数的命令将失败。
-Confirm
提示你在运行 cmdlet 之前进行确认。
Type:
SwitchParameter
Aliases:cf
Position:Named
Default value:False
Accept pipeline input:False
Accept wildcard characters:False
-Independent
指示此 cmdlet 启动独立于任何正在进行的事务的事务。
默认情况下,如果在另一个事务正在进行时使用
Start-Transaction
,则会向正在进行的事务添加新订阅服务器。 仅当会话中已经在执行某项事务时,此参数才有效。
默认情况下,如果在事务正在进行时使用
Start-Transaction
,则会重用现有事务对象并递增订阅者计数。 这与加入原来事务的效果非常类似。 命令
Undo-Transaction
回滚整个事务。
若要完成事务,必须为每个订阅者输入一个
Complete-Transaction
命令。
因为同时执行的多数事务都是相关的,所以默认设置可满足大多数用途的要求。
如果指定
Independent
参数,此 cmdlet 将创建一个新事务,该事务可以在不影响原始事务的情况下完成或撤消。 但是,由于一次只能有一个事务处于活动状态,因此必须完成或回滚新事务,然后才能继续处理原来的事务。
Type:
SwitchParameter
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False
-RollbackPreference
指定自动回滚事务的条件。 此参数的可接受值为:
Error
如果发生终止或非终止错误,则事务会自动回滚。
TerminatingError
如果发生终止错误,则事务会自动回滚。
Never
事务永远不会自动回滚。
默认值为
Error
。
Type:
RollbackSeverity
Accepted values:Error, TerminatingError, Never
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False
-Timeout
指定事务处于活动状态的最长时间,以分钟为单位。 当该超时到期时,将自动回滚事务。
默认情况下,在命令行启动的事务没有超时值。 如果事务由脚本启动,则默认超时值为 30 分钟。
Type:
Int32
Aliases:TimeoutMins
Position:Named
Default value:None
Accept pipeline input:False
Accept wildcard characters:False
-WhatIf
显示运行该 cmdlet 时会发生什么情况。 cmdlet 未运行。
Type:
SwitchParameter
Aliases:wi
Position:Named
Default value:False
Accept pipeline input:False
Accept wildcard characters:False
不能通过管道将输入传递给此 cmdlet。
此 cmdlet 将不生成任何输出。
Complete-Transaction
Get-Transaction
Undo-Transaction
Use-Transaction