Reply to Message

View discussion in a popup

Replying to:
LucD
Leadership
Leadership

Do you mean the password for the account under which the task runs in the Windows Task Scheduler?
Or the password for the account with which you run the Connect-VIServer?

To capture errors inside the script you can use Try-Catch construct.
If a cmdlet should fail with a non-terminating exception, you can always add an -ErrorAction Stop to force a terminating exception.

When an exception occurs, you code will end up in the Catch block, where you could pass another exit code.
Which should also be reflected in the Task in the Windows Task Scheduler.

For example, to capture a non-existing VM, you could do

Try {
   $esx = Get-Cluster -VM $vmName -ErrorAction Stop | Get-VMHost | Get-Random

   # The rest of your code

   exit 0  # All went well
}
Catch {
   exit 1  # Error encountered
}




Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference