Hey everyone,
VMware PowerCLi is a very powerful tool for generating custom reports about your vSphere environment, and automating many of your daily tasks as well.
In this article I will be listing most of the very useful PowerCLi commands and scripts that I use very often, but you need first to be familiar with PowerShell and PowerCLi from VMware to be able to use them; if you aren’t, don’t worry, just search for ‘VMware PowerCLi”, download it, and read about it and how to use it.
Set VM-Notes to multiple VMs:
Purpose: automating a task
This simple script automates the process of defining/updating the Notes of multiple VMs instead of using the vSphere Web Client and wasting your time. This script will read the vm names from a csv file that you will be creating, just set the column header to “VMname” for the VM name; and “Notes” to the column of notes/description. copy the following text and save it in a text file with “ps1” extension to be able to run it as a script:
Get-Content -Path “<SPECIFY-HERE-THE -CSV-FILE\FILE.CSV” | ConvertFrom-Csv -Header VMName,Notes | %{
Get-VM -Name $_.VMName | Set-VM -Notes $_.Notes -Confirm:$false
}
Count the virtual disks of each VM
Purpose: generate a custom report.
Get-VM -name * |Select Name,@{N=’vDisk’;E={($_.ExtensionData.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualDisk]}).Count}}
Here’s an example and the result:
Count vNICs of the VMs
Purpose: generate a custom report.
Get-VM -name *|Select Name,@{N=’vNIC’;E={($_.ExtensionData.Config
.Hardware.Device | where{$_ -is [VMware.Vim.VirtualEthernetCard]}).Count}}
Result:
Get full information about the VMs
Purpose: generate a custom report.
Get-VM -name * |Select Name,@{N=’vDisk’;E={($_.ExtensionData.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualDisk]}).Count}},@{N=’vNIC’;E={($_.ExtensionData.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualEthernetCard]}).Count}}, Notes, Guest, NumCpu, CoresPerSocket, MemoryGB, ResourcePool, UsedSpaceGB, ProvisionedSpaceGB
Example:
you can export the info (more reasonable and readable) to a CSV file by adding the following suffix to the previous command (or any command in general):
| export-csv “<FILEPATH.CSV>”
example:
Get-VM -name * |Select Name,@{N=’vDisk’;E={($_.ExtensionData.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualDisk]}).Count}},@{N=’vNIC’;E={($_.ExtensionData.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualEthernetCard]}).Count}}, Notes, Guest, NumCpu, CoresPerSocket, MemoryGB, ResourcePool, UsedSpaceGB, ProvisionedSpaceGB | export-csv “c:\vm-info.csv”
List host HBA fiber channel adapters and their WWN addresses
Purpose: generate a custom report.
Get-VMHostHBA -Type FibreChannel | Select VMHost,Device,@{N=”WWN”;E={“{0:X}” -f $_.PortWorldWideName}}
Get information about distributed switch port groups, and VLAN ID associated with each one
Purpose: generate a custom report.
for a single VDS:
Get-VDSwitch -Name <VDS-NAME> | Get-VDPortgroup | select -Property name, @{N=”VLANId”;E={$_.Extensiondata.Config.DefaultPortCOnfig.Vlan.VlanId}}
Result
To get the same information, but for all of the vDS switches that you have; run the command as following:
Get-VDSwitch | Get-VDPortgroup | select -Property name, @{N=”VLANId”;E={$_.Extensiondata.Config.DefaultPortCOnfig.Vlan.VlanId}}
Get ESXi host NICs and vmkernel adapters details
Purpose: generate a custom report.
Get-VMHostNetworkAdapter | select VMhost, Name, IP | format-table
-autosize
The “format-table” suffix is used make the table columns fit the data, it;s very handy.
Example:
Get the VM name and the cluster that it belongs to
Purpose: generate a custom report.
Get-VM | Select Name, @{N=”Cluster”;E={Get-Cluster -VM $_}}
Example
Get VM network information
Purpose: generate a custom report.
This command will be listing the VM name, vNIC, and the port group attached to.
Get-VM | Get-NetworkAdapter | Select-Object @{N=”VM”;E={$_.Parent.Name}},@{N=”NIC”;E={$_.Name}},@{N=”Network”;E={$_.NetworkName}} | ft -AutoSize
Example
Get datastore name along with the canonical name.
Purpose: generate a custom report.
Get-Datastore | Select Name,@{N=’CanonicalName’;E={$_.Extensiondata.Info.Vmfs.Extent[0].DiskName}}
Example
Get VM info, IP address, and more
Purpose: generate a custom report.
but it won’t be useful unless .you export it to a CSV file
Get-VM | Select -property Name, @{N=”Cluster”;E={Get-Cluster -VM $_}},@{N=’IP’;E={$_.guest.IPAddress[0]}} , “Guest”, Notes, NumCpu,CoresPerSocket, MemoryGB, ProvisionedSpaceGB , UsedSpaceGB
Get-VM | Select -property Name, @{N=”Cluster”;E={Get-Cluster -VM $_}},@{N=’IP’;E={$_.guest.IPAddress[0]}} , “Guest”#foreach($vm in Get-VM){
# $vm.Guest.IPAddress[0] |
# Select -property Name, @{N=”Cluster”;E={Get-Cluster -VM $_}}, @{N=’IP’;E={$_.guest.IPAddress[0]}} , “Guest”, ProvisionedSpaceGB , UsedSpaceGB, Notes, NumCpu, CoresPerSocket, MemoryGB
#}
#foreach($vm in Get-VM){
# $vm.Guest.IPAddress[0] |
# Select @{N=’Name’;E={$vm.name}},@{N=’IP’;E={$_}}, @{N=”Cluster”;E={Get-Cluster -VM $_}}, @{N=”Operating System”;E={Guest}}, @{N=”Notes”;E={Notes}}, @{N=”CPU Count”;E={NumCpu}}, @{N=”Core Count”;E={CoresPerSocket}}, @{N=”RAM”;E={MemoryGB}}, @{N=”Provisioned Capacity”;E={ProvisionedSpaceGB}}, @{N=”Used Space”;E={UsedSpaceGB}}
#}# ProvisionedSpaceGB , UsedSpaceGB, Notes, NumCpu, CoresPerSocket, MemoryGB
#, UsedSpaceGB
#Guest
#Notes
#NumCpu
#CoresPerSocket#MemoryGB
Example
To export the result to CSV, use the following example:
PS E:\> .\get-ipv4+details.ps1 | Export-Csv “d:\vm.csv”
The result should look like this:
Get VMware tools version and status
Purpose: generate a custom report.
get-vm | select -property @{N=”Name”; E={$_.name}}, ToolsVersion,ToolsVersionStatus
Example
Check the ESXi host domain authentication health status
Purpose: generate a custom report.
Get-VMHostAuthentication | select -Property @{N=”Host”;E={$_.vmhost.Name}}, Domain, DomainMembershipStatus
Example
Join ESXi host(s) to AD domain
Purpose: automate a task.
Get-VMHostAuthentication -VMHost <VMHost> | Set-VMHostAuthentication -JoinDomain -Domain <Domain> -User <Username> -Password <Password>
To apply this to all of your hosts, run the command as following. Bear in mind that the username used for joining to domain must be written in the same format as below:
Get-VMHostAuthentication -VMHost * | Set-VMHostAuthentication -JoinDomain -Domain “homelab.local” -User “user01@homelab.local” -Password <Password>
Refresh/rescan HBA adapters of ESXi hosts
Purpose: automate a task.
Get-VMHostStorage -VMHost <HOSTNAME or ” * ” for all hosts> -Refresh -RescanAllHba -RescanVmfs
Example
Set DNS and NTP server IP addresses to all ESXi hosts
Purpose: automate a task.
save this syntax to a “ps1” file to run it as a script
# PowerCLI Script to Configure DNS and NTP on ESXi Hosts
# PowerCLI Session must be connected to vCenter Server using Connect-VIServer# Prompt for Primary and Alternate DNS Servers
$dnspri = read-host “Enter Primary DNS”
$dnsalt = read-host “Enter Alternate DNS”# Prompt for Domain
$domainname = read-host “Enter Domain Name”#Prompt for NTP Servers
$ntpone = read-host “Enter NTP Server One”
$ntptwo = read-host “Enter NTP Server Two”$esxHosts = get-VMHost
foreach ($esx in $esxHosts) {
Write-Host “Configuring DNS and Domain Name on $esx” -ForegroundColor Green
Get-VMHostNetwork -VMHost $esx | Set-VMHostNetwork -DomainName $domainname -DNSAddress $dnspri , $dnsalt -Confirm:$false
Write-Host “Configuring NTP Servers on $esx” -ForegroundColor Green
Add-VMHostNTPServer -NtpServer $ntpone , $ntptwo -VMHost $esx -Confirm:$false
Write-Host “Configuring NTP Client Policy on $esx” -ForegroundColor Green
Get-VMHostService -VMHost $esx | where{$_.Key -eq “ntpd”} | Set-VMHostService -policy “on” -Confirm:$falseWrite-Host “Restarting NTP Client on $esx” -ForegroundColor Green
Get-VMHostService -VMHost $esx | where{$_.Key -eq “ntpd”} | Restart-VMHostService -Confirm:$false}
Write-Host “Done!” -ForegroundColor Green
Set ESXi shell session time-out
Purpose: automate a task.
In the following example, the timeout is set to 900 seconds, adjust that value to suit your needs. The value is in seconds.
#List UserVars.ESXiShellInteractiveTimeOut for each host
Get-VMHost | Select Name,
@{N=”UserVars.ESXiShellInteractiveTimeOut”;E={$_
| Get-AdvancedSetting -Name
UserVars.ESXiShellInteractiveTimeOut
| Select -ExpandProperty Value}}# Set UserVars.ESXiShellTimeOut to 900 on all hosts
Get-VMHost
| Foreach { Get-AdvancedSetting -Entity $_ -Name
UserVars.ESXiShellInteractiveTimeOut | Set-AdvancedSetting
-Value 900 }
Set/modify DNS servers configured on ESXI hosts
Purpose: automate a task.
Note: this wont work if the host is already a domain member, the host needs to leave the domain first, run this command, and then re-join it to domain again.
Get-VMHost | get-vmhostnetwork | Set-VMHostNetwor k -DnsAddress 192.168.1.50, 192.168.1.151
I hope you find this helpful.
Thank you,
5 responses to “For VMware Admins | day-to-day useful PowerCLi commands/scripts”
Hi
I need to shutdown all the vms in Vcenter with multiple host cluster using powercli…any one help me to get this script ?
Get-VM * | Shutdown-VMGuest
[…] VMware Admins | day to day useful PowerCLI commands / scripts This article has a collection of scripts that cover off a lot of different VMware related things and I like […]
Brilliant one-liner powercli commands.. Your Disk Count one is coming to me handy on my bigger script 🙂 Appreciate your knowledge sharing…
Good luck