PowerCLI Script: VM Info Report

Managing a large environment with hundreds or even thousands of virtual machines is a big challenge. Checking something manually on dozens of VMs is very time consuming. That’s when scripts and automation come in handy.

In this article I will show you a script that will generate a report containing information about all virtual machines in your vCenter. The report will be in Excel format and it will contain basic information about every VM, like VM Name, Power state, CPU count, RAM assigned, OS, Datastore, ESXi, IP address, Cluster and Provisioned space.

The script presented in this article can be found at My GitHub Repository.


Prerequisites

Powershell

Make sure you have the latest version of Powershell. Go to this website and download/upgrade your Powershell.

PowerCLI module

Use the following command to install PowerCLI module in your Powershell.

Install-Module -Name VMware.PowerCLI

Once done, run the following command. This is to avoid potential issues with SSL connection to vCenter.

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore

Script

Modify the script below to add your vCenter IP/Hostname.

You can also modify the location of the output file, the default is C:\temp

Run the script, you will be asked for your vCenter credentials.

# vmcloud.pl
# Connect to vCenter
Connect-VIServer -Server <vCenter_IP>

# Get all VMs in vCenter
$allVMs = Get-VM

# Store VM information
$vmInfo = @()

# Loop through each VM and retrieve its information
foreach ($vm in $allVMs) {
    $vmHost = Get-VMHost -VM $vm
    $vmDisk = $vm | Get-HardDisk
    $vmInfo += [PSCustomObject] @{
        Name = $vm.Name
        PowerState = $vm.PowerState
        NumCPU = $vm.NumCpu
        MemoryMB = $vm.MemoryMB
        GuestOS = $vm.Guest.OSFullName
        IPAddress = ($vm.Guest.IPAddress)[0]
        Cluster = ($vm | Get-Cluster).Name
        Datastore = ($vm | Get-Datastore).Name
        Host = $vmHost.Name
        ProvisionedSpaceGB = [Math]::Round(((($vmDisk.CapacityGB | Measure-Object -Sum).Sum)),2)
    }
}

# Export VM information to a CSV file
$vmInfo | Export-Csv -Path "C:\temp\VM_Info.csv" -NoTypeInformation

# Display a message to confirm that the report has been saved
Write-Host "Report saved to C:\temp\VM_Info.csv"

Results

Running the script:

Result:


Thank you for reading! Here are some links you may be interested in:

Leave a comment

Create a website or blog at WordPress.com

Up ↑