PowerCLIでvCenterサーバに接続(Connect-VIServer)する際のエラー処理


vSphere環境でvCenterサーバにConnect-VIServerで接続する際に、エラーとなった場合に、どのようにするべきか悩んだ。

まずは、単純にtry/catchで書いてみる。

try{
    Connect-VIServer -Server $vcenter -User $vcenterusername -Password $vcenterpassword -WarningAction 0
} catch {
    Write-Host "vCenterサーバへの接続で問題が発生しました。処理を終了します。"
    Write-Host $Error[0]
    exit 1
}

これを$vcenterなどの変数設定なしに実行すると以下のエラーとなり、想定どおりの出力とはなる。

vCenterサーバへの接続で問題が発生しました。処理を終了します。
Cannot validate argument on parameter 'Server'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

しかし、下記のように$vcenterなどに値を入れているが、実はでたらめで接続できなかった場合を試してみると問題が発生した。

$vcenter="testvcenter"
$vcenterusername="administrator@vsphere.local"
$vcenterpassword="test"
try{
    Connect-VIServer -Server $vcenter -User $vcenterusername -Password $vcenterpassword -WarningAction 0
} catch {
    Write-Host "vCenterサーバへの接続で問題が発生しました。処理を終了します。"
    Write-Host $Error[0]
    exit 1
}

実行結果は以下のようになり、catch内が実行されていない。

Connect-VIServer : 2017/02/09 17:55:54    Connect-VIServer        Could not resolve the requested VC server.
Additional Information: There was no endpoint listening at https://testvcenter/sdk that could accept the message. This is often caused by an incorrect address or SOAP a
ction. See InnerException, if present, for more details.    
At line:6 char:5
+     Connect-VIServer -Server $vcenter -User $vcenterusername -Passwor ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [Connect-VIServer], ViServerConnectionException
    + FullyQualifiedErrorId : Client20_ConnectivityServiceImpl_Reconnect_NameResolutionFailure,VMware.VimAutomation.ViCore.Cmdlets.Commands.ConnectVIServer

これを、Connect-VIServerの実行結果を見て、falseだったら例外を発生させる、ということにより、対応させることができた。

$vcenter="testvcenter"
$vcenterusername="administrator@vsphere.local"
$vcenterpassword="test"
try{
    Connect-VIServer -Server $vcenter -User $vcenterusername -Password $vcenterpassword -WarningAction 0
    if($? -eq $false){ throw }
} catch {
    Write-Host "vCenterサーバへの接続で問題が発生しました。処理を終了します。"
    Write-Host $Error[0]
    exit 1
}

誤りの場合の実行例は以下のようになる。

Connect-VIServer : 2017/02/09 18:00:57    Connect-VIServer        Could not resolve the requested VC server.
Additional Information: There was no endpoint listening at https://testvcenter/sdk that could accept the message. This is often caused by an incorrect address 
or SOAP action. See InnerException, if present, for more details.    
At line:6 char:5
+     Connect-VIServer -Server $vcenter -User $vcenterusername -Passwor ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (:) [Connect-VIServer], ViServerConnectionException
    + FullyQualifiedErrorId : Client20_ConnectivityServiceImpl_Reconnect_NameResolutionFailure,VMware.VimAutomation.ViCore.Cmdlets.Commands.ConnectVIServer
 
vCenterサーバへの接続で問題が発生しました。処理を終了します。
ScriptHalted

2018/07/06追記

PowerCLI 10.0以降では、自己証明はデフォルト拒否になりました。 このため、上にあげたスクリプトを実行すると下記の様にエラーとなります。

PS /root> Connect-VIServer -Server IPアドレス -User root -Password password -WarningAction 0
Connect-VIServer : 2018/07/06 14:29:03  Connect-VIServer                Error: Invalid server certificate. Use Set-PowerCLIConfiguration to set the value for the InvalidCertificateAction option to Ignore to ignore the certificate errors for this connection.
Additional Information: Could not establish trust relationship for the SSL/TLS secure channel with authority 'IPアドレス'.
At line:1 char:1
+ Connect-VIServer -Server IPアドレス -User root -Password password - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : SecurityError: (:) [Connect-VIServer], ViSecurityNegotiationException
+ FullyQualifiedErrorId : Client20_ConnectivityServiceImpl_Reconnect_CertificateError,VMware.VimAutomation.ViCore.Cmdlets.Commands.ConnectVIServer

PS /root>

VMware PowerCLI Blog「New Release: VMware PowerCLI 10.0.0」の「Default Certificate Handling」に記載があるように、全体設定として無視するための「Set-PowerCLIConfiguration -InvalidCertificateAction Ignore」を設定する必要があります。

PS /root> Set-PowerCLIConfiguration -InvalidCertificateAction Ignore

Perform operation?
Performing operation 'Update PowerCLI configuration.'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help
(default is "Y"):y

Scope    ProxyPolicy     DefaultVIServerMode InvalidCertificateAction  DisplayD
                                                                       eprecati
                                                                       onWarnin
                                                                       gs
-----    -----------     ------------------- ------------------------  --------
Session  UseSystemProxy  Multiple            Ignore                    True
User                                         Ignore
AllUsers


PS /root>

この処理を実行後に、改めて接続を実行

PS /root> Connect-VIServer -Server IPアドレス -User root -Password password
Name Port User
—- —- —-
IPアドレス 443 root

PS /root>

ConoHaでUbuntu 16.04 64bitを指定したパスワードでデプロイできない件は未解決のままもうすぐ10日経過



ConohaでUbuntu16.04 64bitを構築すると高確率で設定したパスワードが利用できない」の件ですが、2016/12/07 19:23頃の発生よりもうすぐ10日となりますが、何も進展を見せないままとなりました。

進捗に関する連絡も無く、
使えないことに対してまでサーバ費用が発生している件も
こちらから連絡を入れて督促を行わない限り無応答だったりと
なかなかアレなサポート状況となっています。

現時点でもなお解決予定は未定だとか

1GBのやつが45日間安い、というから使おうとしたのに、使えないまま期間終了
果たしてどうなってるんだかねぇ・・・

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:\>

ConohaでUbuntu16.04 64bitを構築すると高確率で設定したパスワードが利用できない



2016/12/07 19:23~2016/12/08 18:00頃の出来事です。

先日Amazon LightsailでやろうとしていたことはRAM 512MBでは実現できないということが判明し、RAM1GBを安く使えるところを探したら、「ConoHa」で45日間810円で使える、というのをやっていることを発見。

じゃぁ、使ってみるかと早速デプロイ
すると、デプロイ時に指定したはずのパスワードでログインできないわけです。

デプロイ時、パスワード指定が1つしかなく、しかもマスクをされるので、入力を間違ってもわからないわけなんですが
その後、50回以上試したところ、Ubuntu 16.04 64bit環境では5回ぐらいしかログインに成功しませんでした。
これがCentOS7.2 64bit環境だと3回中3回成功です。

途中、入力間違いが無いようにコピー&ペーストを使ってみても状況は変わらなかったので、非常に謎。

サポートに問い合わせるも2日経過後も原因不明とのこと。

果たしてConoHaを利用することはできるのでしょうか・・・


以下、メモ書き

1. 「Ubuntu 16.04 64bit」を選択し、rootパスワードとしてとある文字列指定して、仮想インスタンスをデプロイ
2. コンソール画面開いてログインを使用とするも、パスワード違っている、としてログインできない・・・

3. パスワード入力間違ったかなぁ、と、rootパスワードが分からない場合の対処を調べる
4. Conohaの場合、対処はない、らしいので、新しくもう1つの仮想インスタンスをデプロイ
5. うっかり間違えて「Cent OS 7.2 64bit」でデプロイしてしまっていたが、ログインできることを確認
6. もう1つ新しい仮想インスタンスを、「Ubuntu 16.04 64bit」を選択し、デプロイ
7. 今度はログインに成功
8. 最初のは入力し間違いだったのだろう、と納得して、セットアップ開始
9. 途中うっかり操作を誤って、まともに動作しないようにしてしまう
10. 仮想インスタンスを新しく作らなくても「再構築」で作り直せる、というのを知る
11. というわけで、操作を誤った仮想インスタンスに対して「Ubuntu 16.04 64bit」で再構築
12. ログインできない
13. おっかしいなぁ・・・と、再度「再構築」
14. ログインできない
15. エディッタに書いたパスワード文字列をコピー&ペーストして「再構築」
16. ログインできない
17. コンソールでのパスワード文字列入力もコピー&ペーストできるのを知って再ログインを試みるも失敗

最終的に、最低でも40回は再構築したはずで、もしかすると50回を超えてるかも?
それだけの回数を行って、ログインに成功したのは3回程度でした。

で・・・問い合わせを行っているのですが、サポートに連絡をして、受け付けましたという連絡があったのが12/08 10:20

連絡がないのでとりあえずもう1回チャレンジしてみる

それでも連絡がないので、15:55にチャットで連絡をとってみる(下記のツイート画像は9時台ですが、その後も実験しています)

原因不明、解決見込みも不明、とのこと

で・・・そこから丸一日経ちましたが、原因不明、解決見込みも不明、だそうな

ちなみに、パスワード文字列は「ILoveCOnoha1!」というやつでも現象が発生しています。
アンチウイルスソフトやネットワーク環境固有の問題も疑い、3箇所でそれぞれアンチウイルスソフトも違う場所から操作を行っても現象は発生。

いったいなんなのか・・・・

というわけで、無駄に時間を使わせてるワリに、いまだに利用できないConoHaはクソ!

なお、この試行錯誤に使った29円、支払う必要あるの?と聞いたところ、クーポンによる値引きが適用されましたとさ。
(確認しなかったら適用しないつもりだったんだろうか?)

Amazon Lightsailのインスタンスを消す際の手順キャプチャ



Amazon LightsailのRAM 512MBのインスタンスが、1ヶ月目無料、ということなので、いま構築しようとしているメールサーバの検証用に使ってみるかな、と試してみた。

が・・・やってみるとRAM 512MBだとメモリが足らず、必要なソフトウェアが全然起動しなかった。
SSDを使っていることだし、ローカルディスクのswapfile作ってみたらどうなるかな?と試してみたところ、ソフトウェアは起動したものの、今度は負荷でインスタンス全体がまともに動かなくなった・・・

というわけで、RAM512MBでは使い物にならない、ということで、作成したLightsailのインスタンスを消すことにしたので、その手順キャプチャを一挙公開。

ちなみに、構築手順もキャプチャを撮ってはいたものの、どうせ、こんなんは、よくある記事なので省略です。

1. 仮想マシンインスタンスの「Delete」タブを開きます

2. 「Delete」ボタンを押し、表示される確認に「Yes」とこたえます

3. 削除中…

4. 削除完了…なんだけどエラー。これは問題ないエラー表示
いままで「ubuntu-webserver」インスタンスのページを開いていたが、該当インスタンスが削除されたので、そのページが表示出来ない、というエラー。

5. 続いて固定IPの削除をするため「Resouces」の「Networking」にある該当IPアドレスをクリック

6. 「この固定IPが割り当てられていないこと」と「割り当てていない場合、コストがかかること」が警告されているのを確認
割り当てられていない固定IPを保持していると、1時間あたり$0.005の料金が発生しますので、不要ならちゃんと消しましょう。(参考までRAM512MBの仮想インスタンスは1時間当たり$0.007です)

7. 「Delete」タブにて「Delete Static IP」を実行

8. 最終確認を「Yes」

9. 削除が完了しました

で・・・
今回のテストで約4時間インスタンスを立ち上げていたわけなのですが、費用は・・・

・・・いまのところ0ドルですが、24時間経過後あたりで変わったりするんでしょうかね?
う~ん・・・