PowerCLI

 View Only
  • 1.  try catch not catching specific error

    Posted Jan 31, 2020 12:36 PM

    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"
     
    }

    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

     


  • 2.  RE: try catch not catching specific error

    Posted Jan 31, 2020 12:50 PM

    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?



  • 3.  RE: try catch not catching specific error

    Posted Jan 31, 2020 12:58 PM

    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.



  • 4.  RE: try catch not catching specific error

    Posted Jan 31, 2020 12:58 PM

    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.



  • 5.  RE: try catch not catching specific error

    Posted Jan 31, 2020 01:08 PM

    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.



  • 6.  RE: try catch not catching specific error

    Posted Jan 31, 2020 01:54 PM

    Well that doesn't matter, with or without it doesn't catch the error.



  • 7.  RE: try catch not catching specific error

    Posted Jan 31, 2020 02:00 PM

    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?



  • 8.  RE: try catch not catching specific error

    Posted Jan 31, 2020 02:04 PM

    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



  • 9.  RE: try catch not catching specific error

    Posted Jan 31, 2020 02:12 PM

    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

    try {

        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"

        }

    }