VMware Cloud Community
dwchan
Enthusiast
Enthusiast

How to handle hang process with Invoke-VMscript

I am using the following to initiate the VMware OS Optimization Tool on a newly build VM

My-Logger "Execute VMware OS Optimization Tool with defined template..."
$ConfigureOSOT = 'Write-Verbose -Message "Configuring VMware OSOT" -Verbose;
$OSOTtemplate = "VMware Templates\Windows 10 and Server 2016 or later";
$PostInstallFolders = "D:\CustomFolder";
$folder = "$PostInstallFolders\VMware_OSOT";
Set-Location -Path $folder;
.VMwareOSOptimizationTool.exe -o -t "$OSOTtemplate" -r C:\Temp\ -reboot'
Invoke-VMScript -ScriptText $ConfigureOSOT -VM $strVMName -GuestCredential $DCLocalCredential

The OSOT would run and create the log without issue.  However, the OSOT process on the VM would just hang and never exit.  I am not seeing the same issue if I execute the app locally on the VM.  Is there a way to when I execute the 'invoke-vmscript' to include some type of timeout? Or there is another way to approach this with my code?

0 Kudos
3 Replies
LucD
Leadership
Leadership

You have to realise that the script/program you start inside the Guest OS runs in and is controlled by the Guest OS.

And there is no timeout option on the Invoke-VMScript cmdlet.

What you could do is start the script/program inside the Guest OS via Invoke-VMScript with the RunAsync switch.

That way the Invoke-VMScript cmdlet will come back immediately. Now you can keep a timer in your calling script and check on regular intervals if your script/program is still running (with another call to Invoke-VMScript). And eventually, kill it (through another call to Invoke-VMScript) when it exceeds the timeout you envisage.

As you can see, you would have to control this logic from your calling script.


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

0 Kudos
dwchan
Enthusiast
Enthusiast

How do I check if a process exists on the VM from powershell?  

0 Kudos
LucD
Leadership
Leadership

Get-Process


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

0 Kudos