VMware Cloud Community
w1nter
Enthusiast
Enthusiast

annoying error behaviour in scripts when using set-vmhostfirmware

Hi All,

I am writing some jumbo scripts to reset my environments for testing, and am having some problems with catching errors, specifically with the use of the set-vmhostfirmware -resettodefaults cmdlet.

So i have a loop (below) which connects to some esx4i hosts, checks for running vms, shuts them down if needed, puts the hosts into maintenance mode then resets them.

So what i am finding is that about 30% - 40% of the time, the "Set-VMHostFirmware -ResetToDefaults" command times out. It actually succeeds every time (as in the host is reset), but the cmdlet seems to timeout, and results in the following error, which in itself isnt the problem but the script i run bombs out.

      • error ***

21/03/2010 12:35:16 PM Set-VMHostFirmware 52e71a7c-90d3-c974-3112-659f5a7d2a95 The operation has timed out

At :line:531 char:25

+ Set-VMHostFirmware <<<< -ResetToDefaults -ErrorAction SilentlyContinue

      • end error ***

the thing i want to get sorted, is in my loop, i error check pretty much every statement, and log it to various places which is fine. If the 'Connect-ViServer' cmdlet fails, this gets caught no probs, is logged and the loop continues. but if the "set-vmhostfirmware -resettodefaults" cmdlet fails, the script bombs out, the execution stops. it does not execute the next line in the loop, which is intended to log this failure and then just continue.

I have tried using the -erroraction option, but this doesnt change the behavior at all. i just want my loop to run through, even when a cmd for an execution of the loop doesnt work....

why, if the connect-viserver fails, does the loop continue and "if ($? -eq $false)" catches this, but when the set-vmhostfirmware cmdlet fails, does the script execution stop???

Any thoughts would be greatly appreciated!

Kind Regards,

David

foreach ($pod_host in $pod_hosts)

{

Connect-VIServer $pod_host -User root -Password "SomePass!"

if ($? -eq $false)

{

$now = tm ; Write-Output "$now : ERROR : Error Connecting to $pod_host" >> $log

$Action_List = "$Err_Num : Error connecting to $pod_host to do the factory reset" ; $Err_Num+

}

else

{

$now = tm ; Write-Output "$now : INFO : Connected to $pod_host " >> $log

$guests_to_stop = (Get-VM | where {$_.PowerState -eq "PoweredOn"})

if ($guests_to_stop -ne $null)

{

$now = tm ; Write-Output "$now : INFO : Stopping the following guests so we can put $pod_host into Maintenance Mode: $guests_to_stop " >> $log

$guests_to_stop | Stop-VM -Confirm:$false

}

Set-VMHost -State Maintenance

if ($? -eq $false)

{

$now = tm ; Write-Output "$now : ERROR : Error setting $pod_host to maintenance mode." >> $log

$Action_List = "$Err_Num : Error setting $pod_host to maintenance mode" ; $Err_Num+

}

else

{

sleep 10

$now = tm ; Write-Output "$now : INFO : Set $pod_host into maintenance mode." >> $log

Set-VMHostFirmware -ResetToDefaults -ErrorAction SilentlyContinue

if ($? -eq $false)

{

$now = tm ; Write-Output "$now : ERROR : Error resetting $pod_host to factory defaults. Sometimes this times out when it actually does work..." >> $log

$Action_List = "$Err_Num : Error setting $pod_host to factory defaults...may be a furfie." ; $Err_Num+

}

else

{

$now = tm ; Write-Output "$now : INFO : Sucessfully Reset $pod_host to factory defaults." >> $log

}

}

}

}

0 Kudos
1 Reply
jeveenj
Enthusiast
Enthusiast

Hi David,

There are some errors which can be continued silently and some cannot be.

For example:

$i = 1
while($i -le 5){
#with wrong server, works fine
Connect-VIServer 10.10.10.10 -user user -Password password -ErrorAction SilentlyContinue
"its running for $i"
$i++
} 

This will run and give output as:

its running for 1

its running for 2

its running for 3

its running for 4

its running for 5

And when you try this

$i = 1
while($i -le 5){
#with wrong server
Connect-VIServer 10.10.10.10 -user user -Password password -ErrorAction SilentlyContinue

#throws error for this line
Get-VM -ErrorAction SilentlyContinue
"its running for $i"
$i++
}

Throws an error.

The ErrorAction parameter has no effect on terminating errors that prevent a command from completing successfully.

My point is, there are some errors which cannot be continued silently in PowerShell. Might be timed out is one of them.

-If you found this information useful, please consider awarding points for Correct or Helpful.
0 Kudos