VMware Cloud Community
pamiller21
Enthusiast
Enthusiast

Template Update Script Not Finishing

I have a script that takes a template and converts it, updates the windows, then converts it back.  Script works great with Win2016 & 2019, but I had to make a win10 template and it won't convert it back.  Looking at it and I think the problem is that the VM will not start vmtools until someone logs into it and the script is designed to turn off the vm once it sees the tools are initialized.  I don't know what's going on in Window's world that makes this fail but unless I can find a magic wand to make it work I am thinking about making the script just ignore that and continue on.

Script:

#Update Template Parameters

    #Update Template Name

    $updateTempName = "TempWin10"

#---------------------

#Update Template

#---------------------

try {

    #Get Template

    $template = Get-Template $updateTempName

    #Convert Template to VM

    if($showProgress) { Write-Progress -Activity "Update Template" -Status "Converting Template: $($updateTempName) to VM" -PercentComplete 5 }

    [void]$log.appendline("Converting Template: $($updateTempName) to VM")

    $template | Set-Template -ToVM -Confirm:$false

    #Start VM

    if($showProgress) { Write-Progress -Activity "Update Template" -Status "Starting VM: $($updateTempName)" -PercentComplete 20 }

    [void]$log.appendline("Starting VM: $($updateTempName)")

    Get-VM $updateTempName | Start-VM -RunAsync:$RunAsync

    #Wait for VMware Tools to start

    if($showProgress) { Write-Progress -Activity "Update Template" -Status "Giving VM: $($updateTempName) 3200 seconds to start VMwareTools" -PercentComplete 35 }

    [void]$log.appendline("Giving VM: $($updateTempName) 3200 seconds to start VMwareTools")

    sleep 90

    #VM Local Account Credentials for Script

    $cred = New-Object System.Management.Automation.PSCredential $updateTempUser, $updateTempPass

    #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

    "

   

    #Running Script on Guest VM

    if($showProgress) { Write-Progress -Activity "Update Template" -Status "Running Script on Guest VM: $($updateTempName)" -PercentComplete 50 }

    [void]$log.appendline("Running Script on Guest VM: $($updateTempName)")

    Get-VM $updateTempName | Invoke-VMScript -ScriptText $script -GuestCredential $cred

   

    #Wait for Windows Updates to finish after reboot

    if($showProgress) { Write-Progress -Activity "Update Template" -Status "Giving VM: $($updateTempName) 3200 seconds to finish rebooting after Windows Update" -PercentComplete 65 }

    [void]$log.appendline("Giving VM: $($updateTempName) 3200 seconds to finish rebooting after Windows Update")

    sleep 3200

    #Shutdown the VM

    if($showProgress) { Write-Progress -Activity "Update Template" -Status "Shutting Down VM: $($updateTempName)" -PercentComplete 80 }

    [void]$log.appendline("Shutting Down VM: $($updateTempName)")

    Get-VM $updateTempName | Stop-VMGuest -Confirm:$false

    #Wait for shutdown to finish

    if($showProgress) { Write-Progress -Activity "Update Template" -Status "Giving VM: $($updateTempName) 3200 seconds to finish Shutting Down" -PercentComplete 90 }

    [void]$log.appendline("Giving VM: $($updateTempName) 3200 seconds to finish Shutting Down")

    sleep 3200

   

    #Convert VM back to Template

    if($showProgress) { Write-Progress -Activity "Update Template" -Status "Convert VM: $($updateTempName) back to template" -PercentComplete 100 }

    [void]$log.appendline("Convert VM: $($updateTempName) back to template")

    Get-VM $updateTempName | Set-VM -ToTemplate -Confirm:$false

}

catch {

    [void]$log.appendline("Error:")

    [void]$log.appendline($error)

    Throw $error

    #stops post-update copy of template

    $updateError = $true

    }

#---------------------

#End of Update Template

#---------------------

0 Kudos
1 Reply
LucD
Leadership
Leadership

The VMware Tools are normally configured as a service which start automatically when the OS starts.

Did you check that?

You seem to wait 90 seconds in the script after the async power on.

Is that time long enough?

Why not wait till the GuestOperationsReady (in GuestInfo) says $true?

There are several examples of using that property available in this community.


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

0 Kudos