0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PowerShellでトランザクションを使用する

Posted at

背景

powershellスクリプト(ps1)でアイテム削除するスクリプトを書いていて、ふと、削除処理をトランザクション制御できたら便利じゃない?と思ったので。

結論から言うと出来ませんでした。

Start-Transaction

Start-Transactionコマンドレットでトランザクションを開始できます。コミットするにはComplete-Transactionです。
https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-transaction?view=powershell-5.1

このStartとComtleteで挟まれたブロックは、トランザクション制御の対象になります。ただしそのブロック内で使用するコマンドレットがトランザクションに対応してないといけません。
具体的には-UseTransactionパラメータが指定できるかどうかです。

加えて、プロバイダがトランザクションに対応してる必要があります。

プロバイダ?

PowerShellにおけるオブジェクト操作の仕組みです。
https://docs.microsoft.com/ja-jp/powershell/scripting/developer/provider/windows-powershell-provider-overview?view=powershell-7

Windows PowerShell プロバイダーを使用すると、マウントされたドライブのように、任意のデータストアをファイルシステムとして公開できます。

トランザクション制御に対応しているプロバイダはレジストリのみです。
つまり、レジストリに関する操作はトランザクション制御が可能です。
対応しているプロバイダは下記のコマンドレットで調べましょう。

get-psprovider | where {$_.Capabilities -like "*transactions*"}

無題.png

トランザクションで怒られてみる

remove-itemでtransactionを指定してみましょう。
無題.png

怒られました。ファイルシステムがトランザクション非対応です。

まとめ

残念ながらファイルシステムプロバイダがトランザクション制御していませんでした。

いつか対応するんでしょうか?

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?