PowerCLI 6.5以降でAdd-PSSnapinが使えなくなった場合の対処法

PowerCLIを5.xから6.5以降にアップグレードすると,これまで動いていたPowerShellスクリプトが動かなくなるということがあります。

その場合,PowerShellで以下のようなエラーログが出力されると思います。

The Windows PowerShell snap-in 'VMware.VimAutomation.Core' is not installed on this computer.

The Windows PowerShell snap-in ‘VMware.VimAutomation.Core’ is not installed on this computer.

Add-PSSnapin : The Windows PowerShell snap-in 'VMware.VimAutomation.Core' is not installed on this computer.
At line:1 char:1
+ add-pssnapin VMware.VimAutomation.Core
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (VMware.VimAutomation.Core:String) [Add-PSSnapin], PSArgumentException
    + FullyQualifiedErrorId : AddPSSnapInRead,Microsoft.PowerShell.Commands.AddPSSnapinCommand

これは,PowerCLI 6.5以降で,Add-PSSnapinというコマンドが完全に廃止されたことによるエラーです。

では,どうすればよいのでしょうか。

Import-Moduleを代わりに使う

PowerCLI 6.5で実装された主な変更点は,Snap-Inが完全に廃止されModuleを使うようになったことです

それで,Add-PSSnapin VMware.VimAutomation.Core を使う代わりに,Import-Module VMware.VimAutomation.Core を使いましょう。

モジュールをインポートすれば,これまでと同じシンタックスを使うことができます。

また,VMware.VimAutomation.ViCore にも調整が加えられています。Implというタイプが,Typesに代わっていたりしますので,仮想マシンのプロパティを取得する際に使う,VMware.VimAutomation.ViCore.Impl.V1.Inventory.VirtualMachineImpl などは,VMware.VimAutomation.ViCore.Types.V1.Inventory.VirtualMachine のように書き換えて使いましょう。

以上,PowerCLI 6.5以降でAdd-PSSnapinが使えなくなった場合の対処法でした。

コメント