arbelac
Contributor
Contributor

Windows Updating stucking via invoke-vmscript

Hi ,

I am trying windows update. but it is stuck as below. There is no progress.

screenshot:

 

goldenimage.png

 

Script :

 

	#Script to run on VM
	$script = "Function WSUSUpdate {
		  param ( [switch]`$rebootIfNecessary,
				  [switch]`$forceReboot)  
		`$Criteria = ""IsInstalled=0 and Type='Software'""
		`$Searcher = New-Object -ComObject Microsoft.Update.Searcher
		try {
			`$SearchResult = `$Searcher.Search(`$Criteria).Updates
			if (`$SearchResult.Count -eq 0) {
				Write-Output ""There are no applicable updates.""
				exit
			} 
			else {
				`$Session = New-Object -ComObject Microsoft.Update.Session
				`$Downloader = `$Session.CreateUpdateDownloader()
				`$Downloader.Updates = `$SearchResult
				`$Downloader.Download()
				`$Installer = New-Object -ComObject Microsoft.Update.Installer
				`$Installer.Updates = `$SearchResult
				`$Result = `$Installer.Install()
			}
		}
		catch {
			Write-Output ""There are no applicable updates.""
		}
		If(`$rebootIfNecessary.IsPresent) { If (`$Result.rebootRequired) { Restart-Computer -Force} }
		If(`$forceReboot.IsPresent) { Restart-Computer -Force }
	}
	WSUSUpdate -rebootIfNecessary
	"


Invoke-VMScript -VM $updateTempName -ScriptText $script -Verbose -GuestUser $updateTempUser -GuestPassword $updateTempPass -ScriptType Powershell


sleep 5

 

thanks,

0 Kudos
4 Replies
LucD
Leadership
Leadership

Is UAC enabled in that Windows Guest OS?
It could be that the script is hanging with the UAC prompt.


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

0 Kudos
arbelac
Contributor
Contributor

yes already disabled UAC. Now I am working my script. i will inform you tomorrow. 

guest os : server 2016

EDIT1 :  After about 30 minutes , I got the following the error message. But script still working 

Also , I have checked my Guest OS too. 

Invoke-VMScript : 11/26/2022 12:00:04 AM Invoke-VMScript The operation is not allowed in the current state.
At line:133 char:1
+ Invoke-VMScript -VM $updateTempName -ScriptText $script -Verbose -Gue ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-VMScript], InvalidState
+ FullyQualifiedErrorId : Client20_VmGuestServiceImpl_GetProcessOutputInGuest_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Invok
eVmScript

.

 EDIT 2 :  Now, its completed my windows update script. But , still running tiWorker.exe and no any reboot action.

 

goldenimage2.png

 

0 Kudos
LucD
Leadership
Leadership

Are you sure the Guest OS didn't reboot (perhaps check the eventlogs)?
When the Guest OS reboots while an Invoke-VMScript is still running, you might get this "current state" message.
Since the communication with the VMware Tools in the Guest OS is interrupted.

As a tip, I normally run these kind of scripts (that might involve a reboot) via the Windows Task Scheduler.
That way the Invoke-VMScript comes back rather quickly (the code just needs to create the Scheduled Task).
Then I check after some time if the Scheduled Task ran successfully, and did what it had to do.


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

0 Kudos
arbelac
Contributor
Contributor

Are you sure the Guest OS didn't reboot (perhaps check the eventlogs)?

Now, while checking the server, the following screen came up.

goldenimage3.png

Also , Since the communication with the VMware Tools in the Guest OS is interrupted.  This is not possible. Because as I mentioned already script is completed.

 

EDIT1 : btw,  now rebooted and update completed success.

0 Kudos