Hello,
I'm refining my script and trying to handle errors and write nice error messages. But somehow the catch of an specifiy error will not work.
Here's my code:
try {
Connect-VIServer -Server $serverconfig.vCenterServer -ErrorAction Stop
}
Catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidLogin] {
Write-Host "Wrong Credentials"
$vmcheck = 1
}
try {
Disconnect-VIServer * -Confirm -ErrorAction Ignore
}
catch [VMware.VimAutomation.ViCore.Cmdlets.Commands.DisconnectVIServer]{
write-host "no connections to disconnect"
}
what am I doing wrong? How should I handle these kind of error?
Thanks and best
Noah
Since you are not providing the User/Password on the Connect-VIServer cmdlet, the cmdlet will first try the VICredentialStore and then SSPI (your current account).
If neither method provides a valid credential, you will be prompted for user/password.
See also about_server_authentication
What happens when you run this?
Do you get logged on?
Or do you get an error?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Well that's my intention. I then get prompted for my credentials Iwith which I want to connect to the vCenter. If I enter correct credentials the connection get's established. If not it troughs an error and I wan't to catch this error.
Strange... I just tested it again... The catch of the wrong login works now but the disconnect of the vcenter is still not getting caught.
Well that's my intention. I then get prompted for my credentials Iwith which I want to connect to the vCenter. If I enter correct credentials the connection get's established. If not it troughs an error and I wan't to catch this error.
Strange... I just tested it again... The catch of the wrong login works now but the disconnect of the vcenter is still not getting caught.
For me the Connect-VIServer is working correctly when prompted.
On the Disconnect-VIServer you seem to have -ErrorAction Ignore.
That will definitely not cause a terminating error and will never enter the catch-block.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Well that doesn't matter, with or without it doesn't catch the error.
It does matter, the try-catch construct only works with a terminating error.
With Ignore the code will never get into the catch-block.
What kind of error are you getting on the Disconnect-VIServer?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes, sorry I meant for me it doesn't matter if with or without I still get the same error.
This error:
Disconnect-VIServer : 31.01.2020 14:54:35 Disconnect-VIServer Could not find any of the servers specified by name.
In Zeile:2 Zeichen:1
+ Disconnect-VIServer * -Confirm
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (VMware.VimAutom...Server[] Server:RuntimePropertyInfo) [Disconnect-VIServer], ServerObnFailureException
+ FullyQualifiedErrorId : Core_ObnSelector_SetNewParameterValue_ServerSpecifiedButNotFound,VMware.VimAutomation.ViCore.Cmdlets.Commands.DisconnectVIServer
Ok, use the Stop action and use the correct exception type (your's isn't).
The 2nd issue, the "Could not find ..." doesn't generate a specific exception, but the general VimException one.
That means that you will have to test in the catch-block what the specific error is.
Something like this
DisConnect-VIServer -Server * -ErrorAction Stop
}
Catch [VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.VimException] {
if($error[0].Exception.ErrorCategory -eq 'objectNotFound'){
Write-Host "Server not found"
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference