Getting to know PowerCLI with examples

What is PowerCLI? PowerCLI is a Windows PowerShell interface for managing VMware vSphere.  PowerCLI is a powerful command-line tool that lets you automate all aspects of vSphere management, including network, storage, VM, guest OS and more.   PowerCLI is distributed as a Windows PowerShell snapin (PowerCLI 6.0 introduced PowerShell module), and includes over 500 PowerShell cmdlets for managing and automating vSphere and vCloud, along with documentation and samples.  Here are some useful commands to get started.

First you need to download and install PowerCLI.

To import the PowerCLI module run the below:

Import-Module VMware.VimAutomation.Core

 

Once you have that you can connect to your vCenter server using:

Connect-VIServer vCenterServer01

 

You will then be prompted for your vCenter login credentials.  You can then start running commands within PowerCLI.

This command will give you a .csv file containing all snapshots outstanding within your vCenter environment:

get-vm | get-snapshot | export-csv c:\temp\snapshots.csv

 




This script will give you a list of all Datastores and a breakdown of the remaining space:

Get-VM |
Select Name,
@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIdList | Select -ExpandProperty Name))}},
@{N="UsedSpaceGB";E={[math]::Round($_.UsedSpaceGB,1)}},
@{N="ProvisionedSpaceGB";E={[math]::Round($_.ProvisionedSpaceGB,1)}},
@{N="Folder";E={$_.Folder.Name}} |
Export-Csv C:\temp\VMsDatastores.csv -NoTypeInformation -UseCulture

 

This will give you a list of all VMs that are powered on:

Get-vm | where { $_.PowerState -eq PoweredOn} | export-csv c:\temp\VMsOn.csv

 

This will give you IP address, name, notes, folder and guest ID:

Get-vm | where { $_.PowerState -eq PoweredOn} | Select Name,Notes,Folder,GuestID,@{N="IP Address";E={@($_.guest.IPAddress[0])}} | export-csv c:\temp\VMsOn.csv

 

This command will give you a list of all your DRS Affinity rules:

Get-DrsRule | Select Name, Enabled, Type, @{Name="VM"; Expression={ $iTemp = @(); $_.VMIds | % { $iTemp += (Get-VM -Id $_).Name }; [string]::Join(";", $iTemp) }} | export-csv c:\temp\DRSRules.csv

 

This command will show you all the VMs with ISO files mounted:

Get-VM | FT Name, @{Label="ISO file"; Expression = { ($_ | Get-CDDrive).ISOPath }}

 

This command will give you information about your hosts:

get-vmhost | Select Name,@{N=Cluster;E={Get-Cluster -VMHost $_}},@{N=Datacenter;E={Get-Datacenter -VMHost $_}} | Export-csv c:\temp\hostinventory.csv 

 

This will give you a list of outstanding snapshots:

get-vm | get-snapshot | export-csv c:\temp\snapshots.csv 

 




This will give you the free space remaining in all your datastores:

Get-Datastore | Select Name,CapacityGB,FreeSpaceGB,state | sort -Property FreeSpaceGB -Descending | Export-Csv C:\Scripts\vCenter\DatastoreReport.csv -NoTypeInformation -UseCulture 

 

Another command to give datastore information…

get-datastore | select Name,FileSystemVersion,Datacenter,CapacityGB,FreeSpaceGB | Export-csv c:\temp\datastore.csv 

 

This will give you all the thick provisioned VMs in your environment:

Get-Datastore | Get-VM | Get-HardDisk | Where {$_.storageformat -eq "Thick" } | Select Parent, Name, CapacityGB, storageformat | Export-Csv C:\temp\VMsThick.csv -NoTypeInformation -UseCulture 

 

This will give you all the thin provisioned VMs in your environment:

Get-Datastore | Get-VM | Get-HardDisk | Where {$_.storageformat -eq "Thin" } | Select Parent, Name, CapacityGB, storageformat | Export-Csv C:\temp\VMsThin.csv -NoTypeInformation -UseCulture 

 

This command will show you information on your vSwitches and port groups:

Get-VirtualPortGroup | select Name,VirtualSwitch,VLanId,VMHostID | Export-Csv C:\temp\VMHostNetworkInfo.csv

 

This command translates the HostID to the host DNS name:

Get-VMHost | Select ID,name 

 

This will return one specific VM

get-vm -name vmtest | export-csv c:\temp\vmtest.csv

This command will disconnect your PowerShell session from the vCenter server:

Disconnect-VIServer servername -confirm:$false

 

This command will take information from multiple vCenters and give you VM and Host information:

Connect-VIServer vcenter01

Connect-VIServer vcenter02

Connect-VIServer vcenter03

Connect-VIServer vcenter04

Get-vm | where { $_.PowerState -eq PoweredOn} | export-csv c:\temp\VMsOn.csv

get-vmhost | Select Name,@{N=Cluster”;E={Get-Cluster -VMHost $_}},@{N=Datacenter”;E={Get-Datacenter -VMHost $_}},MemoryTotalGB,NumCpu,MaxEVCMode,Manufacturer,ProcessorType,Version | Export-csv c:\temp\hostinventory.csv 

One Reply to “Getting to know PowerCLI with examples”

Leave a Reply

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