Tag Archives: Patching

I am often rebuilding my lab environment and finding that I want to run windows update on all of the guest vms in it. This in its own is not a problem but I don’t want to set up a WSUS server or wait for all the clients to do the normal Windows Update check-in Cycle.

This module works great but it had a few things missing for what I wanted.

If updates were needed after the first boot you had to manually run the module / script again

If you don’t have Microsoft Update enabled you are not able to patch anything other than core OS updates.

If you don’t have Use Include Recommended there would still be updates that could be missed.

Please don’t misunderstand me there is nothing wrong with the module itself but, for my list of requirements the module would not meet everything that I wanted to do. So out came some more PowerShell and in a few hours all my requirements have been met.

So first issue was to enable the Microsoft Update module remotely. This one is the easiest of the list above to fix. There is a COM object for Microsoft Update that allows you to enable Microsoft Update programmatically. For me the best way to update this is to just PowerShell remote in to the target machine and update the service.

So now we can remotely enable Microsoft Update service on any machine that we can remotely execute PowerShell on. Task two done.

Ok working on the list of tasks the 3rd was the 2nd easiest to get going. This one took a bit of creative thinking but no big deal. I was able to use the base of the code above and switching the COM object to Microsoft.Update.AutoUpdate you can enable the includeRecommendedUpdates.

This time I got a whole bunch of red hhmmm this works on local machine but not on remote.

Time to open up my search engine and try to find out what is going on. Turns out that while the code is sound it seems that Microsoft has not enabled remote access to the Microsoft.update.autoupdate objects. (I knew that you could not remotely call windows update but I didn’t think about changing settings having the same issue.)

So the best way to get around this is to enable the setting using a scheduled task. The good news about doing this is the Invoke-WUInstall command from PSWindowsUpdate already does this so all that we need to do is add this script into the script that Invoke-WUInstall calls. In my case I wanted to be able to set IncludeRecommended by variable.

Now when we call the Invoke-WUinstall it will create a remote scheduled task on the computer. One of the commands that it will execute will be to enable Recommended updates.

Ok so two parts of the script are done. Now for the most fun one. I want to have the scheduled task run multiple times on the remote machine until either the number of times that I have set have run or all updates are applied.

The first step is to add in a trigger to the job that PSWindowsUpdate created. As the machines that I am wanting to update could be running anything back to windows 2008 R2 and will most likely have PowerShell 3.0 installed I could only use the scheduler Com components to update the task.

Instead of having to manually copy the script out to all of the targeted machines, I wanted to include it in my original script with the ability to write it out to my target machine, if required. The best way to do that is to use a here string in PowerShell. When you are doing this you will just need make to sure that if you want to add in variables that you add the ` in front of the $ otherwise PowerShell will put the values for the variables in at run time and not write them out as you may want . I have only included a small snippet of the script here string showing that I added in some parameters and also checking if the destination folder that the script is to be written in exists.

So that was task one done. At this stage it is now complete. I have a script that checks and enables Windows Update, Auto Updates, Recommended updates on remote machines allowing them to reboot a number of times and re-run updates until they are fully patched.