Inventory VMware virtual machines

For reporting purposes, I maintain an Excel sheet containing information on my server base. I update this sheet on a weekly basis. Part of the information displayed in this sheet comes from a PowerShell script I have created to extract the required data from our vCenters.

This script pulls the following information for every VM running in the vCenter environment:

  • VM Name
  • hardware version
  • power status
  • vCenter it runs on (in case you have more than 1)
  • ESXi host the VM runs on
  • cluster the VM is assigned to
  • VMware tools status
  • guest OS
  • number of vCPU’s
  • is CPU HotAdd enabled?
  • amount of assigned RAM
  • is memory HotAdd enabled?
  • number of disks assigned to the VM
  • disk sizes
  • number of NICs
  • NIC types (E1000, VMNIC3)
  • NIC IP addresses
  • NIC MAC addressess
  • the folder it lives in

Maybe this script is sufficient for your needs. If not, it should be straight forward enough for you to adjust. A requirement for this script to work is the VMware PowerCLI PowerShell module, which can be installed with this PowerShell command:

Install-Module -Name VMware.PowerCLI -Scope CurrentUser

See https://blogs.vmware.com/PowerCLI for more information about VMware PowerCLI.

# Import VMware PowerCLI module
If (-not (Get-Module -Name 'VMware.PowerCLI')) {
    Import-Module VMware.PowerCLI
}

# Variable declaration
$Report = New-Object System.Collections.ArrayList
$DateTime = $((Get-Date).ToString('yyyy-MM-dd_hh-mm-ss'))

# Set report output path
$OutputPath = "$env:USERPROFILE\Documents\Reports"
If ( -Not (Test-Path -Path $OutputPath)) {
    New-Item -ItemType directory -Path $OutputPath
}
$Outputfile = "All-VMs-$DateTime"

# Get credentials to use to connect to the vCenter(s)
Clear-Host
$Credentials = Get-Credential -Message "Please enter the username and password to use to connect to the vCenter(s)"

# Connect to vCenter(s)
$ListOfvCenters = (
    "<FQDN/IP address of vCenter #1",
    "vcenter01.yourdomain.local",
    "10.10.10.50"
)
Connect-VIServer $ListOfvCenters -Credential $Credentials -ErrorAction Continue

# Get a list of all ESXi hosts running in the vCenter(s)
$ListOfVMs = $ListOfVMs | Sort-Object -Property Name              # Sort the VM list on alphabetical order
#$ListOfVMs = Get-VM | Where-Object {$_.Name -like "*text*"}   # User this line to get info on specified VM's

# Get required information for each host
$Count = $ListOfVMs.Count
$Counter = 1

ForEach ($VM in $ListOfVMs) {
    Write-Host "Processing VM $VM [$Counter/$Count]"

    $VMView = $VM | Get-View
    $VMNICs = (Get-NetworkAdapter -VM $VM)
    $VMDisks = Get-HardDisk -VM $VM

    $Object = [PSCustomObject]@{
        Name                = $VM.Name
        PowerState          = $VM.PowerState
        HardwareVersion     = $VM.HardwareVersion
        vCenter             = $VM.Uid.Substring($VM.Uid.IndexOf('@') + 1).Split(":")[0]
        Host                = $VM.VMHost.Name
        Cluster             = $VM.VMHost.Parent.name
        ToolsStatus         = $VMView.Guest.ToolsStatus
        GuestOS             = (Get-VMGuest -VM $VM).OSFullName
        NumCPU              = $VM.NumCpu
        CpuHotAddEnabled    = ($VM | Select-Object ExtensionData).ExtensionData.config.CpuHotAddEnabled
        MemGB               = $VM.MemoryGB
        MemoryHotAddEnabled = ($VM | Select-Object ExtensionData).ExtensionData.config.MemoryHotAddEnabled
        NumDisks            = $VMDisks.Count
        Disk1SizeGb         = $VMDisks.CapacityGb[0]
        Disk2SizeGb         = $VMDisks.CapacityGb[1]
        Disk3SizeGb         = $VMDisks.CapacityGb[2]
        Disk4SizeGb         = $VMDisks.CapacityGb[3]
        NumNICs             = $VMNICs.Count
        Nic0_Type           = $VMNICs.Type[0]
        NIC0_IP             = $VM.Guest.IPAddress[0]
        NIC0_Mac            = $VMNICs.MacAddress[0]
        Nic1_Type           = $VMNICs.Type[1]
        NIC1_IP             = $VM.Guest.IPAddress[1]
        NIC1_Mac            = $VMNICs.MacAddress[1]
        Nic2_Type           = $VMNICs.Type[2]
        NIC2_IP             = $VM.Guest.IPAddress[2]
        NIC2_Mac            = $VMNICs.MacAddress[2]
        Folder              = $VM.Folder.Name
    }

    $Report.add($Object) | Out-Null
    $Counter++
}

$Report | Export-Csv "$Outputfile.csv" -NoTypeInformation -UseCulture

$DefaultVIServers | Disconnect-VIServer -Confirm:$False

As always, please keep in mind this script is tailored to my environment, but can be used as a template for your environment. I do not pretend to be a PowerShell guru and as such my script may not be perfect. I am open to suggestions 🙂 . If you found this script useful, I’d appreciate it if you leave a comment.

2 thoughts on “Inventory VMware virtual machines”

Leave a Reply

Your email address will not be published. Required fields are marked *