0

Get VMInformation Using Powershell and PowerCLI

It’s always a good idea to get a high level overview of what’s going on with your VMware environment. When it comes to VMs, the command Get-VM, has a lot of useful information regarding your virtual machines. Today we’re going to develop a script that builds on top of that. The Get VMInformation script is useful if you want an export of where everything is located, network names and guest OS. This Powershell script will even check if vmtools is up to date so you can see which VMs need an update. I had a lot of fun developing the script so let’s get to it.

Get VMInformation Using Powershell and PowerCLI

Function Get-VMInformation {
<#
.SYNOPSIS
    Get information from a VM object. Properties inlcude Name, PowerState, vCenterServer, Datacenter, Cluster, VMHost, Datastore, Folder, GuestOS, NetworkName, IPAddress, MacAddress, VMTools


.NOTES   
    Name: Get-VMInformation
    Author: theSysadminChannel
    Version: 1.0
    DateCreated: 2019-Apr-29


.EXAMPLE
    For updated help and examples refer to -Online version.


.LINK
    https://thesysadminchannel.com/get-vminformation-using-powershell-and-powercli -
    
#>

    [CmdletBinding()]

    param(
        [Parameter(
            Position=0,
            ParameterSetName="NonPipeline"
        )]
        [Alias("VM")]
        [string[]]  $Name,


        [Parameter(
            Position=1,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true,
            ParameterSetName="Pipeline"
            )]
        [PSObject[]]  $InputObject

    )


    BEGIN {
        if (-not $Global:DefaultVIServer) {
            Write-Error "Unable to continue.  Please connect to a vCenter Server." -ErrorAction Stop
        }

        #Verifying the object is a VM
        if ($PSBoundParameters.ContainsKey("Name")) {
            $InputObject = Get-VM $Name
        }

        $i = 1
        $Count = $InputObject.Count
    }

    PROCESS {
        if (($null -eq $InputObject.VMHost) -and ($null -eq $InputObject.MemoryGB)) {
            Write-Error "Invalid data type. A virtual machine object was not found" -ErrorAction Stop
        }

        foreach ($Object in $InputObject) {
            try {
                $vCenter = $Object.Uid -replace "[email protected]"; $vCenter = $vCenter -replace ":.+"
                [PSCustomObject]@{
                    Name        = $Object.Name
                    PowerState  = $Object.PowerState
                    vCenter     = $vCenter
                    Datacenter  = $Object.VMHost | Get-Datacenter | select -ExpandProperty Name
                    Cluster     = $Object.VMhost | Get-Cluster | select -ExpandProperty Name
                    VMHost      = $Object.VMhost
                    Datastore   = ($Object | Get-Datastore | select -ExpandProperty Name) -join ', '
                    FolderName  = $Object.Folder
                    GuestOS     = $Object.ExtensionData.Config.GuestFullName
                    NetworkName = ($Object | Get-NetworkAdapter | select -ExpandProperty NetworkName) -join ', '
                    IPAddress   = ($Object.ExtensionData.Summary.Guest.IPAddress) -join ', '
                    MacAddress  = ($Object | Get-NetworkAdapter | select -ExpandProperty MacAddress) -join ', '
                    VMTools     = $Object.ExtensionData.Guest.ToolsVersionStatus2
                }

            } catch {
                Write-Error $_.Exception.Message

            } finally {
                if ($PSBoundParameters.ContainsKey("Name")) {
                    $PercentComplete = ($i/$Count).ToString("P")
                    Write-Progress -Activity "Processing VM: $($Object.Name)" -Status "$i/$count : $PercentComplete Complete" -PercentComplete $PercentComplete.Replace("%","")
                    $i++
                } else {
                    Write-Progress -Activity "Processing VM: $($Object.Name)" -Status "Completed: $i"
                    $i++
                }
            }
        }
    }

    END {}
}

 

Copy the script to a folder and let’s go over some of the usage and examples. Since Get-VM already displays the basic info such as CPU, memory etc.. in the default output, I didn’t think it was necessary to add those properties in the script.

Get-VMInformation Examples and Usage

First things first, we’ll need to load the function into memory and connect to a vcenter server in order to query the information that we’re looking for. If you’re not connected, the script will output an error saying its unable to continue.

Get VMInformation - Connect to a vcenter server Error - PowerCLI

#import the PowerCLI module
Import-Module VMware.PowerCLI

#Connect to a vcenter server
Connect-VIServer -Server vcenter.thesysadminchannel.com

#Load the function into memory.  This is assuming your file is located in C:\Scripts
. 'C:\Scripts\Get-VMInformation.ps1'

#Get vm information for PAC-DC01.  
PS C:\> Get-VMInformation -Name PAC-DC01

Name        : PAC-DC01
PowerState  : PoweredOn
vCenter     : vcenter.thesysadminchannel.com
Datacenter  : US
Cluster     : Cluster-01
VMHost      : esx.thesysadminchannel.com
Datastore   : ESX_Local
Folder      : VMs
GuestOS     : Microsoft Windows Server 2012 (64-bit)
NetworkName : DPortGroup
IPAddress   : 10.0.0.100
MacAddress  : xx:xx:xx:xx:xx:xx
VMTools     : guestToolsCurrent

 

Since the function allows for pipeline input, we can get an array in VMs from a cluster or datacenter and just pipe it to the function. We will just have to make sure the Get-VM cmdlet is used before the Get-VMInformation to get the objects.

#EXAMPLE 1 - Gets the VM information for all VMs that are in the specified cluster within the US Datacenter and VCSA01 vcenter server.
    Get-Cluster Cluster-01 -Server vCSA01 -Location US | Get-VM | Get-VMInformation

#EXAMPLE 2 - Gets VM information for VMs 1 and 2
    Get-VM VM1, VM2 | Get-VMInformation

#EXAMPLE 3 - Gets VM information for VMs 1 and 2
    Get-VMInformation -Name VM1, VM2

 

I hope you found as much value in the Get-VMInformation script as I did. Let me know in the comments if you would like me to develop a script that has some usefulness to the general population.

For more VMware related articles take a look at VMware Gallery or stop by our Youtube Channel with VMware Videos

Paul

Hi, my name is Paul and I am a Sysadmin who enjoys working on various technologies from Microsoft, VMWare, Cisco and many others. Join me as I document my trials and tribulations of the daily grind of System Administration.