PowerCLI Script to list Windows 2012 R2 VMs only

Version 1

    #################################################

    #

    # PowerCLI Script to list Win2012 R2 VMs

    # Created by BLiebowitz on 8/18/16

    #

    #################################################

     

     

    # Variables

    $vcenter = YOUR VCENTER NAME

     

     

    # Connect to vCenter

      connect-viserver $vcenter

     

     

    # Get Windows 2012 VMs.

      Get-VM | Where{$_.Guest.OSFullName -match '2012'} |

     

     

    # Sort R2 VMs from R1.

      Select Name,

        @{N='OS';E={

     

     

    # Script will prompt for credentials for each VM.  This was done as I have multiple domains in my environment.

            Invoke-VMScript -VM $_ -GuestCredential ($Host.UI.PromptForCredential("Please enter credentials", "Enter Guest

     

     

    credentials for $_", "", "")) -ScriptText {Get-WmiObject -class Win32_OperatingSystem |

      select -ExpandProperty Caption} |

            select -ExpandProperty ScriptOutput}} | Where{$_.OS -match '2012 R2'} |

    # Export list to CSV

      Export-Csv -Path c:\vmware\Win2012R2_VMs.csv -NoTypeInformation -UseCulture