Quantcast
Channel: OSAKANA TAROのメモ帳
Viewing all articles
Browse latest Browse all 816

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

$
0
0

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

Viewing all articles
Browse latest Browse all 816

Trending Articles