List HotFixes using PowerCLI on all Windows VMs in vCenter Environment

List HotFixes using PowerCLI on all Windows VMs in vCenter Environment

List HotFixes using PowerCLI

List HotFixes using PowerCLI – Have you ever wondered how can i get the current patch level for all your windows vm’s in my VMware environment, well the first thought which would come into your mind is to login to all those 100’s of vm’s and manually get the patch level or if you have thought ahead you would go and purchase an enterprise patch management tool ( Which is the preferred way ).

But what if you don’t have the budget or you need this info really fast!!, That’s when powercli and powershell come to the rescue 🙂

Untitled

Here’s a script which can help you get the job done, the first part of the script we would be extracting the list of current hotfixes across all windows vm’s in your vsphere environment and in the second part we would get the list of missing patches!

So Here we go!!

Below script would get the list of all current patches in your vSphere environment.

The script assumes that you have a VISession already established to your vcenter server. Or the best way is to code it in your script itself by passing the $vcenterserver and $credential as parameters.

So once we are connected to the vcenter server, lets start building our script, i have broken down the script with detailed explanation.

 First lets get the credential to authenticate into the windows vm’s ( Assuming you have a service account which has the required privileges and is having sufficient rights to extract the info )

Next comes the real fun, creating VI properties for the properties you want to extract, this can save you lots of time where in you would try to create a custom property using powershell {l=’ineedthis’;e={$_.stuff}}

We will create VI properties for GuestFamily, GuestOSType, GuestHostName and GuestHostIP, this would allow us to extract these properties easily with select statement without complex custom properties.

Next Lets get the above created property details for all windows VM’s across our vcenter environment

Next Lets get into the juice of the script, create an Array which would save all the properties. After that we would iterate across all windows VM’s and get the hotfix installed and its associated properties, also you can see that i am using a try catch statement to filter out the hosts which are not reachable ( you can also out these hostnames to a notepad file (out-file c:\failedvms.txt)).

I also used the write-progress cmdlet to track the current progress of the script with which it would show the admin a transparent UI on whats going on and which VMs are being queried for patches.

Untitled

Once the script completes all your hotfix information would be residing in $array variable which you can now easily export to a CSV 😉

Untitled

Ok so now that’s done, lets head on to our second use case – Extract the list of missing patches across your windows servers, for this we would using the MBSA (Microsoft Baseline Security Analyzer) tool from microsoft, a nice little tool which queries a computer against the security patches which Microsoft recommends to install and gives us a report on the missing patches.

MBSA Tool Output

Untitled

The current script assumes that you have installed the 64 bit version of the MBSA tool.

So lets explore the script, most parts of the script remain the same including creating the VI properties, let dig in right to the core!

We would start the similar way – extract all the details of windows vm’s in the vcenter environment, next i would extract the username and password from the credential object and use it to run the mbsa tool against the VM’s, also note that i am using two progress bars here, one which gives a live view on how much time elapsed from the time the script started and another one which shows the current VM being queried aganist by MBSA. Once the MBSA Tool generates the reports we would convert them into an XML format and extract the required information about missing patches and add them into an array.Also note the usage of a try-catch statement which will catch the hosts which are not reachable.

 

Once the script completes all your missing hotfix information would be residing in $arraymbsa variable which you can now easily export to a CSV 😉 ($arraymbsa | Export-Csv c:\hfix.csv -NoTypeInformation)

List HotFixes using PowerCLI

List HotFixes using PowerCLI

I hope you enjoyed this blogpost on “List HotFixes using PowerCLI” and found this information useful 🙂

Please follow and like us:

Share this post

2 thoughts on “List HotFixes using PowerCLI on all Windows VMs in vCenter Environment

Post Comment