Rashida11
Contributor
Contributor

VMWare PowerShell Error checking

Connect-VISerevr ""

i need to run an error check to see if logged on ok ???

0 Kudos
2 Replies
Niket
Enthusiast
Enthusiast

Hi,

Please refer the below error check method

Connect-VIServer -Server -ErrorAction silentlycontinue -ErrorVariable ErrorString

if ($ErrorString)

{

// Process in case of error }

else

{

// Process in case of no error}

Thanks

Niket

0 Kudos
ggochkov
VMware Employee
VMware Employee

Another way doing it is

  1. Set the error action preference 'Stop', so all the terminating and non-terminating errors will be logged, and moreover all the non-terminating erros will become terminating so you can trap for the errors:

$ErrorActionPreferenceOld = $ErrorActionPreference

$ErrorActionPreference = 'Stop'

Connect-ViServer -Server $srv -User $usr -Password $pass

trap {

#handle the error $_

}

#restore the error action preference

$ErrorActionPreference = $ErrorActionPreferenceOld

The difference between the two approaches is that this terminates the command immediately after the error occurs:

1. Connect-VIServer -Server (<invalid_server>, <server>) -User <user> -Password

<pwd> -ErrorAction silentlycontinue -ErrorVariable ErrorString

- will try to connect to both servers (will not terminate) and will store the error in the ErrorString variable

2. $ErrorActionPreference = 'Stop'

Connect-VIServer -Server (<invalid_server>, <server>) -User <user> -Password

<pwd>

- will stop on the first server and will report the error

0 Kudos