vSphere PowerCLIの一部コマンドがPowerShell ISEで動作しない 2016/12/15版

2017/02/27追記
Windows環境以外でも動作するPowerCLI Coreが登場し、この手順を行おうとしたら、モジュール名が異なり実施できませんでした。
PowerCLIとPowerCLI Coreの双方で動くPowerShellスクリプトの作り方」に、対処方法を記載しています。


1年前に「vSphere PowerCLIの一部コマンドがPowerShell ISEで動作しない」というのを書いた。

最近、環境を作り直すのと一緒にPowerShellを最新にして、そして、PowerCLIを最新の6.5にしてみた。(New Release: PowerCLI 6.5 R1)

以前と同じように、PowerShell ISE上で「Add-PSSnapin VMware.VimAutomation.Core」を実行すると、「Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 5.」というエラーが発生。
「Add-PSSnapin」は使わないようだ。

 C:\Users\test3> Add-PSSnapin VMware.VimAutomation.Core


Add-PSSnapin : No snap-ins have been registered for Windows PowerShell version 5.
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
 

 C:\Users\test3> 

代替はなんだろ?と確認すると「Import-Module -Name VMware.VimAutomation.Core」。

いままでスクリプトにAdd-PSSnapin~と書いていたところを、どちらでも対応できるように書き換えるとすると、下記のようになるだろうか?

if ( $PSVersionTable.PSVersion.Major -lt 5){
    Add-PSSnapin VMware.VimAutomation.Core
}else{
    Import-Module -Name VMware.VimAutomation.Core
}

下記は参考資料の実行ログ

PS C:\Users\test3> Get-Module

ModuleType Version    Name                                ExportedCommands                                                                           
---------- -------    ----                                ----------------                                                                           
Script     1.0.0.0    ISE                                 {Get-IseSnippet, Import-IseSnippet, New-IseSnippet}                                        
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...}                         
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}                                  



PS C:\Users\test3> Get-Module -ListAvailable


    Directory: C:\Program Files\WindowsPowerShell\Modules


ModuleType Version    Name                                ExportedCommands                                                                           
---------- -------    ----                                ----------------                                                                           
Binary     1.0.0.1    PackageManagement                   {Find-Package, Get-Package, Get-PackageProvider, Get-PackageSource...}                     
Script     1.0.0.1    PowerShellGet                       {Install-Module, Find-Module, Save-Module, Update-Module...}                               


    Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules


ModuleType Version    Name                                ExportedCommands                                                                           
---------- -------    ----                                ----------------                                                                           
Manifest   1.0.0.0    AppLocker                           {Set-AppLockerPolicy, Get-AppLockerPolicy, Test-AppLockerPolicy, Get-AppLockerFileInform...
Manifest   1.0.0.0    BitsTransfer                        {Add-BitsFile, Remove-BitsTransfer, Complete-BitsTransfer, Get-BitsTransfer...}            
Manifest   1.0.0.0    CimCmdlets                          {Get-CimAssociatedInstance, Get-CimClass, Get-CimInstance, Get-CimSession...}              
Script     1.0.0.0    ISE                                 {New-IseSnippet, Import-IseSnippet, Get-IseSnippet}                                        
Manifest   1.0.0.0    Microsoft.PowerShell.Archive        {Compress-Archive, Expand-Archive}                                                         
Manifest   3.0.0.0    Microsoft.PowerShell.Diagnostics    {Get-WinEvent, Get-Counter, Import-Counter, Export-Counter...}                             
Manifest   3.0.0.0    Microsoft.PowerShell.Host           {Start-Transcript, Stop-Transcript}                                                        
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Content, Clear-Content, Clear-ItemProperty, Join-Path...}                             
Script     1.0        Microsoft.PowerShell.ODataUtils     Export-ODataEndpointProxy                                                                  
Manifest   3.0.0.0    Microsoft.PowerShell.Security       {Get-Acl, Set-Acl, Get-PfxCertificate, Get-Credential...}                                  
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Format-List, Format-Custom, Format-Table, Format-Wide...}                                 
Manifest   3.0.0.0    Microsoft.WSMan.Management          {Disable-WSManCredSSP, Enable-WSManCredSSP, Get-WSManCredSSP, Set-WSManQuickConfig...}     
Manifest   1.0.0.0    NetworkSwitchManager                {Disable-NetworkSwitchEthernetPort, Enable-NetworkSwitchEthernetPort, Get-NetworkSwitchE...
Manifest   1.1        PSDesiredStateConfiguration         {Set-DscLocalConfigurationManager, Start-DscConfiguration, Test-DscConfiguration, Publis...
Script     1.0.0.0    PSDiagnostics                       {Disable-PSTrace, Disable-PSWSManCombinedTrace, Disable-WSManTrace, Enable-PSTrace...}     
Binary     1.1.0.0    PSScheduledJob                      {New-JobTrigger, Add-JobTrigger, Remove-JobTrigger, Get-JobTrigger...}                     
Manifest   2.0.0.0    PSWorkflow                          {New-PSWorkflowExecutionOption, New-PSWorkflowSession, nwsn}                               
Manifest   1.0.0.0    PSWorkflowUtility                   Invoke-AsWorkflow                                                                          
Manifest   1.0.0.0    TroubleshootingPack                 {Get-TroubleshootingPack, Invoke-TroubleshootingPack}                                      


    Directory: C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Modules


ModuleType Version    Name                                ExportedCommands                                                                           
---------- -------    ----                                ----------------                                                                           
Binary     6.0.0.0    VMware.DeployAutomation                                                                                                        
Binary     6.0.0.0    VMware.ImageBuilder                                                                                                            
Binary     6.5.0.4... VMware.VimAutomation.Cis.Core                                                                                                  
Binary     6.5.0.4... VMware.VimAutomation.Cloud                                                                                                     
Manifest   6.5.0.4... VMware.VimAutomation.Common                                                                                                    
Binary     6.5.0.2... VMware.VimAutomation.Core           HookGetViewAutoCompleter                                                                   
Binary     6.0.0.0    VMware.VimAutomation.HA                                                                                                        
Binary     7.0.2.4... VMware.VimAutomation.HorizonView                                                                                               
Binary     6.5.0.4... VMware.VimAutomation.License                                                                                                   
Binary     6.5.0.4... VMware.VimAutomation.PCloud                                                                                                    
Manifest   6.5.0.4... VMware.VimAutomation.Sdk            Get-PSVersion                                                                              
Binary     6.5.0.4... VMware.VimAutomation.Storage                                                                                                   
Binary     6.5.0.4... VMware.VimAutomation.Vds                                                                                                       
Binary     6.5.0.4... VMware.VimAutomation.vROps                                                                                                     
Binary     6.0.0.0    VMware.VumAutomation                                                                                                           



PS C:\Users\test3> Import-Module -Name VMware.VimAutomation.Core

 C:\Users\test3> Get-Module

ModuleType Version    Name                                ExportedCommands                                                                           
---------- -------    ----                                ----------------                                                                           
Script     0.0        Initialize-VMware_VimAutomation_Cis                                                                                            
Script     1.0.0.0    ISE                                 {Get-IseSnippet, Import-IseSnippet, New-IseSnippet}                                        
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...}                         
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-Type, Clear-Variable, Compare-Object...}                                  
Binary     6.5.0.4... VMware.VimAutomation.Cis.Core       {Connect-CisServer, Disconnect-CisServer, Get-CisService}                                  
Manifest   6.5.0.4... VMware.VimAutomation.Common                                                                                                    
Binary     6.5.0.2... VMware.VimAutomation.Core           {Add-PassthroughDevice, Add-VirtualSwitchPhysicalNetworkAdapter, Add-VMHost, Add-VMHostN...
Manifest   6.5.0.4... VMware.VimAutomation.Sdk            Get-PSVersion                                                                              



 C:\Users\test3> 

なお、PowerCLI 6.5を起動した場合に読み込まれているモジュール一覧は以下の通り

          Welcome to VMware PowerCLI!

Log in to a vCenter Server or ESX host:              Connect-VIServer
To find out what commands are available, type:       Get-VICommand
To show searchable help for all PowerCLI commands:   Get-PowerCLIHelp
Once you've connected, display all virtual machines: Get-VM
If you need more help, visit the PowerCLI community: Get-PowerCLICommunity

       Copyright (C) VMware, Inc. All rights reserved.


PowerCLI C:\> Get-Module

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     0.0        Initialize-VMware.VimAutomation....
Script     0.0        Initialize-VMware.VimAutomation....
Script     0.0        Initialize-VMware.VimAutomation....
Script     0.0        Initialize-VMware_DeployAutomation
Script     0.0        Initialize-VMware_VimAutomation_Cis
Script     0.0        Initialize-VMware_VumAutomation
Manifest   3.1.0.0    Microsoft.PowerShell.Management     {Add-Computer, Add...
Manifest   3.1.0.0    Microsoft.PowerShell.Utility        {Add-Member, Add-T...
Binary     6.0.0.0    VMware.DeployAutomation             {Add-DeployRule, A...
Binary     6.0.0.0    VMware.ImageBuilder                 {Add-EsxSoftwareDe...
Binary     6.5.0.4... VMware.VimAutomation.Cis.Core       {Connect-CisServer...
Binary     6.5.0.4... VMware.VimAutomation.Cloud          {Add-CIDatastore, ...
Manifest   6.5.0.4... VMware.VimAutomation.Common
Binary     6.5.0.2... VMware.VimAutomation.Core           {Add-PassthroughDe...
Binary     6.0.0.0    VMware.VimAutomation.HA             Get-DrmInfo
Binary     7.0.2.4... VMware.VimAutomation.HorizonView    {Connect-HVServer,...
Binary     6.5.0.4... VMware.VimAutomation.License        Get-LicenseDataMan...
Binary     6.5.0.4... VMware.VimAutomation.PCloud         {Connect-PIServer,...
Manifest   6.5.0.4... VMware.VimAutomation.Sdk            Get-PSVersion
Binary     6.5.0.4... VMware.VimAutomation.Storage        {Copy-VDisk, Expor...
Binary     6.5.0.4... VMware.VimAutomation.Vds            {Add-VDSwitchPhysi...
Binary     6.5.0.4... VMware.VimAutomation.vROps          {Connect-OMServer,...
Binary     6.0.0.0    VMware.VumAutomation                {Add-EntityBaselin...


PowerCLI C:\>

“vSphere PowerCLIの一部コマンドがPowerShell ISEで動作しない 2016/12/15版” への1件の返信

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください