The power of the script: vSphere CLI

When you learn the syntax, using the vSphere CLI is an easy way to gather information quickly and easily from one or many vCenters.

PowerCLI is a potent tool. The examples provided only begin to demonstrate its capabilities. Tasks you perform repeatedly or find cumbersome in the web client are ideal for scripting with PowerCLI. Many users are introduced to PowerCLI because they need to automate a task, execute an action based on other actions within vSphere, or handle repetitive tasks that are ideally suited for scripting.

I personally use Macbook, but since not everyone uses this OS I have added how to install the vSphere CLI on Windows as well.

Install CLI on Windows

Go to https://developer.broadcom.com/tools/vsphere-cli/latest

Choose the version you want to use, download it and

Install CLI on MacOS

o install vSphere CLI on a MacBook, you can use VMware’s PowerCLI, which is a command-line interface for managing VMware environments. PowerCLI is built on PowerShell, which is available for macOS. Here are the steps to install it:

Install PowerShell:

  • First, you need to install PowerShell on your Mac. You can do this using Homebrew, a package manager for macOS. Open Terminal and run the following command:
brew install --cask powershell

Install PowerCLI:

  • Once PowerShell is installed, you can install PowerCLI by opening a PowerShell session. Type `pwsh` in Terminal to start PowerShell.
  • In the PowerShell session, run the following command to install PowerCLI:powershellCopy
Install-Module -Name VMware.PowerCLI -Scope CurrentUser

Verify Installation:

  • After installation, you can verify that PowerCLI is installed by
Get-Module -Name VMware.PowerCLI -ListAvailable

Configure PowerCLI:

  • You may need to configure PowerCLI to use specific versions of Python if required by your environment. This can be done using the `Set-PowerCLIConfiguration` cmdlet.

Connect to vCenter

From a MacBook start PowerShell by running the command ” pwsh ” in the Terminal windoe

Use the command below to connect to a vCenter of your choice

Connect-VIServer -Server  <vcenter-server> -User  <username> -Password  <password>

For Passwords, it can be helpful to add ‘ ‘ before and after.

Useful commands

List of all VMs running on a vCenter

#Get list of all VMs unfiltered
Get-VM

#Get info on specific VM
Get-VM -VMname

Getting list of Powered off VMs

Get-VM | where-object {$_.PowerState –eq “PoweredOff”}

Getting the list of networks in a Cluster

# Connect to the vCenter server
Connect-VIServer -Server "your_vcenter_server" -User "your_username" -Password "your_password"

# Get the cluster
$cluster = Get-Cluster -Name "YourClusterName"

# Get all hosts in the cluster
$hosts = Get-VMHost -Location $cluster

# Retrieve network information for each host
foreach ($host in $hosts) {
    Get-VirtualPortGroup -VMHost $host | Select-Object @{Name="Host";Expression={$host.Name}}, Name, VLanId
}

# Disconnect from the vCenter server
Disconnect-VIServer -Confirm:$false

Exporting VMs and their resources to a CSV

# Connect to the vCenter server
Connect-VIServer -Server "your_vcenter_server" -User "your_username" -Password "your_password"

# Retrieve all VMs and their details
$vmInfo = Get-VM | ForEach-Object {
    $vm = $_
    $vmHost = Get-VMHost -VM $vm
    $cluster = Get-Cluster -VM $vm
    PSCustomObject@{
        Name = $vm.Name
        CPU = $vm.NumCpu
        MemoryGB = $vm.MemoryGB
        GuestOS = $vm.Guest.OSFullName
        IPAddress = ($vm.Guest.IPAddress -join ', ')
        Cluster = $cluster.Name
        ESXiHost = $vmHost.Name
    }
}

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

# Disconnect from the vCenter server
Disconnect-VIServer -Confirm:$false

Exporting System information from multiple vCenters

# Define the vCenter servers and credentials
$vCenters = @(
    @{Server = "vcenter1"; User = "user1"; Password = "password1"},
    @{Server = "vcenter2"; User = "user2"; Password = "password2"}
)

# Store VM information
$allVMInfo = @()

# Connect to each vCenter and gather VM information
foreach ($vCenter in $vCenters) {
    Connect-VIServer -Server $vCenter.Server -User $vCenter.User -Password $vCenter.Password

    $vms = Get-VM
    foreach ($vm in $vms) {
        $vmHost = Get-VMHost -VM $vm
        $cluster = Get-Cluster -VM $vm
        $vmInfo = PSCustomObject@{
            Name = $vm.Name
            CPU = $vm.NumCpu
            MemoryGB = $vm.MemoryGB
            GuestOS = $vm.Guest.OSFullName
            IPAddress = ($vm.Guest.IPAddress -join ', ')
            Cluster = $cluster.Name
            ESXiHost = $vmHost.Name
            vCenter = $vCenter.Server
        }
        $allVMInfo += $vmInfo
    }

    Disconnect-VIServer -Server * -Force -Confirm:$false
}

# Export the details to a CSV file
$allVMInfo | Export-Csv -Path "C:\All_VM_Info.csv" -NoTypeInformation

Leave a Reply

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

Share on Social Media