PowerCLI and PowerShell cheat sheet

Here are commands, every vSphere admin should know:

  • List of available interfaces and physical network cards (vmkernel + vmnic):
Get-VMHostNetworkAdapter -vmhost hostname
  • Adding another interface (vmkernel to vMotion) in ESXi:
New-VMHostNetworkAdapter -VMHost "hostname" -PortGroup "portgroupname" -VirtualSwitch "vDS-NAME" -IP 10.XXX.XXX.XXX -SubnetMask 255.XXX.XXX.XXX -VMotionEnabled:$true
  • Removing a specific interface (vmkernel) on ESX:
Get-VMHostNetworkAdapter -vmhost hostname -name vmk0 | Remove-VMHostNetworkAdapter
  • To check if IPv6 is enabled:
Get-VMHost hostname| Get-VMHostNetwork | Select VMHost,IPv6Enabled
  • To enable IPv6:
Get-VMHostNetwork esxihostname | Set-VMHostNetwork -IPv6Enabled:$true

NOTE: After turning IPv6 on/off. reboot is required

  • Listing information about hosts in vCenter (cluster, HW, ESXi version):
Get-VMHost  | select parent,name,Manufacturer,Model,ProcessorType,version
  • Updating VMware Tools without reboot:
Get-VMGuest -VM $vm | Update-Tools -NoReboot
  • Managing host state:
  • Removing unnecessary vMotion “netstack” from the host:
$vmhost = Get-VMHost -Name hostname
$esxcli = Get-EsxCli -VMhost $vmhost -V2
$esxcli.network.ip.netstack.remove.invoke(@{netstack = "vmotion"})
  • Turning on and off the VM on a specific host:
$credESX = get-credential root

$esx = Connect-VIserver -Server -Credential $credESX

Start-VM -Server $esx -VM $vm -Confirm

Stop-VM -server $esx  -VM $vm -kill
  • Managing snapshots:
  • Make a simple clone of a specific virtual machine:
New-VM -Name source_machine -VM target_machine -Datastore datastore_name -vmhost hostname -DiskStorageFormat thin
  • ESXi configuration backup:
Connect-VIServer -Server $esxihost -user $esxihostuser -password $esxihostpasswd

Get-VMHostFirmware -VMHost $esxihost -BackupConfiguration -DestinationPath D:\
  • Enable and disable SSH on all hosts in the cluster:
Get-Cluster CLUSTERNAME | Get-VMHost | Get-VMHostService |Where key -Match tsm-ssh | where Running -Match false | Start-VMHostService -Confirm:$false

Get-Cluster CLUSTERNAME | Get-VMHost | Get-VMHostService |Where key -Match tsm-ssh | where Running -Match true | Stop-VMHostService -Confirm:$false

  • How to ensure TLS1.2 is enabled:
  • Listing events in vCenter with the word “restarted” in their content. OBJECTIVE: to identify machines that restarted after ESXi failure:
Get-VIEvent -MaxSamples 2000 -Start (Get-Date).AddDays(-1) -Type Warning  | Where {$_.FullFormattedMessage -match "restarted"} |select CreatedTime,ObjectName,FullFormattedMessage| sort CreatedTime -Descendin
  • Searching for entries in events in vCenter:
foreach ($vmm in  get-vm  *  ) {
$vmm |Get-VIEvent -Start 2020.03.23  -MaxSamples 1000 | ? FullFormattedMessage -Match Ethernet0 | select {$vmm.Name} , CreatedTime,FullFormattedMessage 
  • Lists the VMware Tools installed on the cluster pointing to the name of the VM and the host:
Get-Cluster *cluster_name* | Get-VM | Get-CDDrive | where{$_.IsoPath -match "vmware/isoimages"} | %{$_.parent } | select name,vmhost
  • Reconfiguring host for HA:

Please like and share to spread the knowledge in the community.

If you want to chat with me please use Twitter: @AngrySysOps

Join my  VMware Knowledge Base Group: https://bit.ly/3w54tbc

Visit my FB page: https://www.facebook.com/AngrySysOps

Subscribe to my channel : https://bit.ly/3vY16CT

Please leave the comment