PowerCLI

 View Only
Expand all | Collapse all

Using PowerCLI to display ONLY WindowsOS and patch status

  • 1.  Using PowerCLI to display ONLY WindowsOS and patch status

    Posted Oct 05, 2015 10:22 PM

    I am trying to find a script that will allow me to use vSphere PowerCLI to list ONLY VMs that are running Windows Operating Systems whether they are powered on or not. I also want to see if those OS's require patching. Does anyone have any idea what script will work best?



  • 2.  RE: Using PowerCLI to display ONLY WindowsOS and patch status

    Posted Oct 06, 2015 12:16 PM

    Good Morning!

    Here is a start. One question I have is whether or not you are running WSUS on your network or not.  Personally I think it would be better to use powershell to query your WSUS server with the vmname vs. querying the Machine.  The reason being that WSUS is your central management location and should be your system of record for the patches.

    Here is a way to get the list of VM's that are windows with their power state.

    $vms = get-vm

    foreach($vm in $vms) {

    $vmview = $vm | get-view

    if ($vmview.Summary.Config.GuestFullName -like "*Windows*"){

      $vm

      }

    }

    Now, when/if you want to search for the patch info, you have two options with  line 5.

    1. Use the $vm name to then invoke a script on the VM to determine patch actions.

    2. Use the $vm name to query your WSUS server.

    Let me know if you have more questions.



  • 3.  RE: Using PowerCLI to display ONLY WindowsOS and patch status

    Posted Oct 06, 2015 04:58 PM

    Thanks! But how can I run updates on all my machines at once? I am running WSUS by the way, so would I be telling my WSUS to force patching?



  • 4.  RE: Using PowerCLI to display ONLY WindowsOS and patch status

    Posted Oct 06, 2015 05:10 PM

    Well again, you can force this via Group Policy (assuming your machines are on a domain) or you can run Different types of scripts that can force machines to update.

    .vbs - https://msdn.microsoft.com/en-us/library/aa387102(VS.85).aspx

    .ps1 http://www.gregorystrike.com/2011/04/07/force-windows-automatic-updates-with-powershell/ or http://www.itnotes.eu/?p=1882

    It kinda depends on your comfort level. 



  • 5.  RE: Using PowerCLI to display ONLY WindowsOS and patch status

    Posted Oct 06, 2015 05:49 PM

    Will this force updates to Machines that are also powered off?



  • 6.  RE: Using PowerCLI to display ONLY WindowsOS and patch status

    Posted Oct 06, 2015 06:28 PM

    No, Windows VM's must be turned on in order to install updates.  You could add the logic to your script to power a vm on, if it's power-state is off.



  • 7.  RE: Using PowerCLI to display ONLY WindowsOS and patch status

    Posted Oct 06, 2015 08:33 PM

    What would that look like? I'm not too familiar with scripting..



  • 8.  RE: Using PowerCLI to display ONLY WindowsOS and patch status

    Posted Oct 07, 2015 08:06 AM

    "LucD Its not liking Get-VM -Name (Missing an argument for parameter 'Name'. Specify a parameter of type 'System.String[]' and try again."

    Which version of PowerCLI do you have installed? Have you imported the module/snapin?


    "Get-WmiObject win32_operatingsystem | Select-Object Name,ServicePackMajorVersion,ServicePackMinorVersion"

    "What would that look like? I'm not too familiar with scripting.."

    This would give you the OS name and SP version installed. You can try it out in powershell on your desktop to see the result.


    If you're using WSUS, I would go with jpsider‌'s suggestion to update servers.



  • 9.  RE: Using PowerCLI to display ONLY WindowsOS and patch status

    Posted Oct 06, 2015 01:37 PM

    Try something like this, but be warned that querying the Update status can take a long time.

    The script uses Invoke-VMscript, so VMware Tools shall be installed on the VMs

    $cmd = @'

    $uSession = New-Object -ComObject Microsoft.Update.Session

    $uSearch = $uSession.CreateUpdateSearcher()

    $uSearch.Search("IsInstalled=0") |

    Select -ExpandProperty Updates |

    Select -ExpandProperty Count

    '@

    Get-VM |

    where{$_.Guest.GuestFamily -match "windows"} |

    Select Name,@{N='Updates missing';E={

        Invoke-VMScript -VM $_ -ScriptText $cmd | Select -ExpandProperty ScriptOutput

    }}



  • 10.  RE: Using PowerCLI to display ONLY WindowsOS and patch status

    Posted Oct 06, 2015 05:01 PM

    LucD Its not liking Get-VM -Name (Missing an argument for parameter 'Name'. Specify a parameter of type 'System.String[]' and try again.



  • 11.  RE: Using PowerCLI to display ONLY WindowsOS and patch status

    Posted Oct 06, 2015 06:47 PM

    Hi

    Try this command

    Get-WmiObject win32_operatingsystem | Select-Object Name,ServicePackMajorVersion,ServicePackMinorVersion



  • 12.  RE: Using PowerCLI to display ONLY WindowsOS and patch status

    Posted Oct 07, 2015 12:12 PM

    You can install Windows WSUS and create a GPO to point to WSUS that will report this.  WSUS is free.  It's actually built into Server 2012.