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?
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
How do I check if a process exists on the VM from powershell?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference