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>