VMware Cloud Community
dwchan
Enthusiast
Enthusiast

Error handling suggestion

Want to get feedback from the group as to what are some common/best approach for error handle when invoking a 'invoke-vmscript" command where something the VM may be busy or just doesn't want to run .  For example, with my code

My-Logger "Install and configure App Volume Agent..."
$ConfigureAppVol = 'Write-Verbose -Message "Configuring App Volume Agent" -Verbose;
$AppVolAgent = "App Volumes Agent.msi";
$AppVolServer = "AppVol.tataoui.com";
$AppVolConfig = "/i ""D:\CustomFolder\VMware_AppVol\$AppVolAgent"" /qn REBOOT=ReallySuppress MANAGER_ADDR=$AppVolServer MANAGER_PORT=443";
Start-Process msiexec.exe -ArgumentList $AppVolConfig -PassThru -Wait'
Invoke-VMScript -ScriptText $ConfigureAppVol -VM $strVMName -GuestCredential $DCLocalCredential

Once in awhile, instead of getting positive feedback where the code was executed
ScriptOutput
-----------------------------------------------------------------------------------------------------------------------|
VERBOSE: Configuring App Volume Agent
|
| Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
| ------- ------ ----- ----- ------ -- -- -----------
| 15 2 320 788 0.00 1496 0 msiexec
|
|
|
---------------------------------------------------------------------------------------------------------------------

I will end up with a very common error like this 
Invoke-VMScript : 11/17/2020 6:37:44 PM Invoke-VMScript The guest operations agent could not be contacted.
At D:\VMware\New-IsoWindowsUnattendedPlusVMwareTools5-Linux.ps1:419 char:9
+ Invoke-VMScript -ScriptText $ConfigureAppVol -VM $strVMName - ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-VMScript], GuestOperationsUnavailable
+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_RunScriptInGuest_ViError,VMware.VimAutomation.ViCore.Cmdlets.Comman
ds.InvokeVmScript

and that portion of the script will get skip.  However, when I get back later and re-run, it will be perfectly fine.  To spot the error, I have to scroll back to review the logs and manually rerun each section.  I can either add something like "start-sleep 10" after each install to let everything come to a steady state, or something in powershell / powercli where if an error would occur, it can either decide to wait and try again, or log it to a different log so I can see the overall result much easier.  Any feedback or suggestion would be appreciated 

0 Kudos
4 Replies
Zsoldier
Expert
Expert

Maybe do a try catch statement.

My-Logger "Install and configure App Volume Agent..."
$ConfigureAppVol = 'Write-Verbose -Message "Configuring App Volume Agent" -Verbose;
$AppVolAgent = "App Volumes Agent.msi";
$AppVolServer = "AppVol.tataoui.com";
$AppVolConfig = "/i ""D:\CustomFolder\VMware_AppVol\$AppVolAgent"" /qn REBOOT=ReallySuppress MANAGER_ADDR=$AppVolServer MANAGER_PORT=443";
Start-Process msiexec.exe -ArgumentList $AppVolConfig -PassThru -Wait'
try{
Invoke-VMScript -ScriptText $ConfigureAppVol -VM $strVMName -GuestCredential $DCLocalCredential
}
catch
{
Write-Host $error[0]
Sleep 10
Invoke-VMScript -ScriptText $ConfigureAppVol -VM $strVMName -GuestCredential $DCLocalCredential
}

 

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
0 Kudos
dwchan
Enthusiast
Enthusiast

simple enough to add and try.  Will add this and do some test run.  Stay tune 😉

0 Kudos
LucD
Leadership
Leadership

Instead of doing a try-catch, you could test in a loop, eventually with a timeout to avoid an endless loop, if the GuestOperationsReady property in the GuestInfo object returns $true.
A simple example, without a timeout.

while (-not $vm.ExtensionData.Guest.GuestOperationsReady)
    {
      Start-Sleep 2
      $vm.ExtensionData.UpdateViewData('Guest')
    }

 


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

LucD
Leadership
Leadership

Oh, and btw, if a cmdlet produces a non-terminating error, it will not jump into the catch block.
You can force a non-terminating error to become a terminating error by adding the -ErrorAction Stop parameter.


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