FYI: Run Auto Windows Updates Without Killing Guests

Discussions related to using VirtualBox on Windows hosts.
Post Reply
scottgus1
Site Moderator
Posts: 20945
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Windows, Linux

FYI: Run Auto Windows Updates Without Killing Guests

Post by scottgus1 »

If Windows Updates is set to fully automatic, it may reboot the host while guests are running, killing the guests and any important data they're working on.

What I do is run Windows Updates from a script I got and modified from a Microsoft site, posted below. A backup batch file can shut down all the guests with Vboxmanage, back them up if desired, then run this VBS script to get and install updates, then reboot. To allow the host desktop to notify the user there are updates, Windows Update settings can be set to Notify before Download, because the script downloads them itself. The script seems to always pull the same updates that are marked "Important" in the desktop Windows Updates window, but not the optional ones, and it automatically accepts any EULAs.

This line in my batch .cmd file runs the script:

Code: Select all

cscript.exe drive:\path\to\windowsupdateauto.vbs
Here's the contents of "windowsupdateauto.vbs"

Code: Select all

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set shell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
scriptpath = fso.getparentfoldername(wscript.scriptfullname) & "\"
reportfilename = scriptpath & "WindowsUpdateReport.txt"

Set updateSession = CreateObject("Microsoft.Update.Session")
updateSession.ClientApplicationID = "MSDN Sample Script"

Set updateSearcher = updateSession.CreateUpdateSearcher()

WScript.Echo "Searching for updates..." & vbCRLF

Set searchResult = _
updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0 and BrowseOnly=0")

WScript.Echo "List of applicable items on the machine:"

For I = 0 To searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(I)
    WScript.Echo I + 1 & "> " & update.Title
Next

If searchResult.Updates.Count = 0 Then
    WScript.Echo "There are no applicable updates."
    WScript.Quit
End If
'wscript.quit

WScript.Echo vbCRLF & "Creating collection of updates to download:"

Set updatesToDownload = CreateObject("Microsoft.Update.UpdateColl")

For I = 0 to searchResult.Updates.Count-1
    Set update = searchResult.Updates.Item(I)
    addThisUpdate = false
    If update.InstallationBehavior.CanRequestUserInput = true Then
        WScript.Echo I + 1 & "> skipping: " & update.Title & _
        " because it requires user input"
    Else
        If update.EulaAccepted = false Then
            update.AcceptEula()
            addThisUpdate = true
        Else
            addThisUpdate = true
        End If
    End If
    If addThisUpdate = true Then
        WScript.Echo I + 1 & "> adding: " & update.Title 
        updatesToDownload.Add(update)
    End If
Next

If updatesToDownload.Count = 0 Then
    WScript.Echo "All applicable updates were skipped."
    WScript.Quit
End If
    
WScript.Echo vbCRLF & "Downloading updates..."

Set downloader = updateSession.CreateUpdateDownloader() 
downloader.Updates = updatesToDownload
downloader.Download()

Set updatesToInstall = CreateObject("Microsoft.Update.UpdateColl")

rebootMayBeRequired = false

WScript.Echo vbCRLF & "Successfully downloaded updates:"

For I = 0 To searchResult.Updates.Count-1
    set update = searchResult.Updates.Item(I)
    If update.IsDownloaded = true Then
        WScript.Echo I + 1 & "> " & update.Title 
        updatesToInstall.Add(update) 
        If update.InstallationBehavior.RebootBehavior > 0 Then
            rebootMayBeRequired = true
        End If
    End If
Next

If updatesToInstall.Count = 0 Then
    WScript.Echo "No updates were successfully downloaded."
    WScript.Quit
End If

If rebootMayBeRequired = true Then
    WScript.Echo vbCRLF & "These updates may require a reboot."
End If

    WScript.Echo "Installing updates..."
    Set installer = updateSession.CreateUpdateInstaller()
    installer.Updates = updatesToInstall
    Set installationResult = installer.Install()
 
    'Output results of install
set resultfile = fso.opentextfile(reportfilename,forwriting,true)
    resultfile.writeline "Listing of updates installed and results:"  
    For I = 0 to updatesToInstall.Count - 1
        resultfile.writeline cstr(I + 1) & "> " & updatesToInstall.Item(I).Title & ": " & installationResult.GetUpdateResult(I).ResultCode   
    Next
    resultfile.writeline "Windows Update Result: " & installationResult.ResultCode 
resultfile.close
    WScript.Echo "Reboot Required: " & installationResult.RebootRequired 
I just tested this on my office Windows 7 host (11-20-2014) to make sure it was good and it worked perfectly.

Also see here: viewtopic.php?f=1&t=61861 for how to run a Vboxmanage on any guest or running guests without knowing ahead of time which guests exist or are running.
GrahamS
Posts: 8
Joined: 21. Nov 2014, 13:48

Re: FYI: Run Auto Windows Updates Without Killing Guests

Post by GrahamS »

Hi,

OK just getting to grips with Virtualbox - and am VERY impressed to-date ;-)). 4.3.18 on a Win8.1 host.

Used Microsoft Virtual PC many years back, and VMWare when doing some contract development - 7 years+ back, so have seen and used other systems.

Seems to me that the functionality being discussed here is FUNDAMENTAL to the safe use of VirtualBox - so surely this support must be built in :-O.

Whilst I accept that there is no way that a system can close cleanly if someone pulls the power, but many users are probably running this on laptops anyway (or UPS-backed servers) - where its pretty obvious when the battery is dying.

There are always valid reasons why a system will be shut down/rebooted. Users, windows updates (assuming a Windows host) etc. etc.

So - there should be a simple way in the Virtual Manager app (or some other flavour of front end) to define what happens to your VM's.

I am currently set up to run numerous different VMs, including Windows (many version) and Linux (Ubuntu) - on a laptop.

It works GREAT :-)) - and I have built up a number of VMs - as I do various types of development.

Now I am at the same stage as a few others seem to be - worrying about what happens when MS decides to roll out an update. NB In some versions this cannot be blocked, and a forced reboot occurs - Server 2012 being one such!! - happens to me on a hosted 2012 server :-O).

In fact my system did this a couple of days back - reboot started whilst manually doing updates, which started me on this path.

An Ubuntu session was 'Paused' but the Manage app said it was Powered off, and refused to allow any control of the app. The VM was in fact still up on the screen and was completely happy to 'Unpause' and continue to run - the Manager (which I had to restart) still saying that the VM was OFF.

To solve the issue - I had to power down the Ubuntu session, then the Manager allowed me to restart it.

Had my system really shut down however - I'm not sure how I would have got control back again, as the VM wouldn't then have still been running.....but been left is a 'paused' state ??

What 'seemed' to happen was that the Manager app got shut down - leaving the VM paused.

So - a clean shutdown feature is required - unless its there and we haven't (yet) found it.

I have tried to find ways to do this and many solutions have been proposed for the last few years :-O - in fact so old as to be ignorable/irrelevant for 4.3.18 ??.

Sorry this turned into such a diatribe :-O - but if it is built in - can someone please explain it to us ??? - or at least point to it in the docs :-O.

Regards

Graham
AnrDaemon
Posts: 134
Joined: 7. Feb 2010, 23:41
Primary OS: MS Windows 7
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Ubuntu Linux, Windows

Re: FYI: Run Auto Windows Updates Without Killing Guests

Post by AnrDaemon »

GrahamS wrote:Seems to me that the functionality being discussed here is FUNDAMENTAL to the safe use of VirtualBox - so surely this support must be built in :-O.
No sane administrator leave their system to automatically install any crap someone else decide to push into it.
Thus, the problem discussed here could only happen for careless system administrators, who deserve everything they can get.
jm
Posts: 1
Joined: 22. Jul 2015, 19:19

Re: FYI: Run Auto Windows Updates Without Killing Guests

Post by jm »

AnrDaemon wrote: No sane administrator leave their system to automatically install any crap someone else decide to push into it.
Thus, the problem discussed here could only happen for careless system administrators, who deserve everything they can get.
But an admin hosting servers isn't the only usecase.

I'm a developer, and I use virtual when I am developing or testing code that runs in different O/S's. I don't have control over when Group Policy pushes an update to my corporate workstation and forces a reboot. I would like for virtual box to close down my guests cleanly though.

If memory serves, VMWare workstation let me set an option to either send the shutdown signal, or to save state when the host O/S shuts down. Would really love that feature in VirtualBox.
scottgus1
Site Moderator
Posts: 20945
Joined: 30. Dec 2009, 20:14
Primary OS: MS Windows 10
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Windows, Linux

Re: FYI: Run Auto Windows Updates Without Killing Guests

Post by scottgus1 »

I don't have control over when Group Policy pushes an update to my corporate workstation and forces a reboot
What I'd do in this case is find out what process runs the updates. Then I'd run a monitor script in the background watching for running processes. Once that update process begins, the monitor script would launch a save-state or shutdown script that safely and quickly shuts down my guests.

I agree that some way of shutting down guests before the host does an automatic shutdown would be good. Must be some really thorny problem - I'm sure the developers would have put that in long ago of it was easy.
noteirak
Site Moderator
Posts: 5231
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: FYI: Run Auto Windows Updates Without Killing Guests

Post by noteirak »

The short story here is that the Host OS will only wait a short amount of time for programs to exit, and then just kill them. Saving state or shutting down a guest takes time, more than what the OS would give you, so you would end up with a forced power off VM anyway.
Like scott said, the issue lies with the forced reboot without any prompt to the user, or the user leaving its VMs running when it's not on the computer, and the small care put in the group policy script of course.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
AnrDaemon
Posts: 134
Joined: 7. Feb 2010, 23:41
Primary OS: MS Windows 7
VBox Version: VirtualBox+Oracle ExtPack
Guest OSses: Ubuntu Linux, Windows

Re: FYI: Run Auto Windows Updates Without Killing Guests

Post by AnrDaemon »

jm wrote:I'm a developer, and I use virtual when I am developing or testing code that runs in different O/S's. I don't have control over when Group Policy pushes an update to my corporate workstation and forces a reboot. I would like for virtual box to close down my guests cleanly though.
If running VM's is a part of your daily job, you can always talk to your system administrators or raise an issue with their superiors to have it resolved to everyone's content.
As the last resort, you can always find a better job, with sane system administrators.
Post Reply