ESXi Powershell Commandlets
From DFWLPiki
Contents
- 1 Scope of This Document
- 2 Install Powershell PowerCli Modules
- 3 Controlling SSH service
- 4 VMware Tools Update without Reboot (reboot must be performed later of course)
- 5 Automatic VMtools Upgrade/Check at Boot
- 6 Get all VMs that have RDM disks in use
- 7 Find all VMs with thick provisioned drives
- 8 Find all VMs that have other than vmxnet3 network adapter
- 9 Connect to your vCenter
- 10 Get all VMs that have snapshots
- 11 Removing a snapshot from a specific VM
- 12 Check for any attached CDRom drives
- 13 Disconnect any attached CDRom drives
- 14 Shutting down or powering up a VM
Scope of This Document
This document covers ESXi powershell commands that I find useful
Install Powershell PowerCli Modules
Install-Module -Name VMware.PowerCLI -AllowClobber Set-PowerCLIConfiguration -Scope AllUsers -ParticipateInCeip $false -InvalidCertificateAction Ignore
Controlling SSH service
get-cluster dfw-mx740c-new | Get-VMHost | Get-VMHostService | Where { $_.Key -eq "TSM-SSH" } | select VMHost, Label, Running get-cluster dfw-mx740c-new | Get-VMHost | Get-VMHostService | Where { $_.Key -eq "TSM-SSH" } | Start-VMHostService get-cluster dfw-mx740c-new | Get-VMHost | Get-VMHostService | Where { $_.Key -eq "TSM-SSH" } | Stop-VMHostService
VMware Tools Update without Reboot (reboot must be performed later of course)
Show All VMs in Cluster that need VMtools Update
Get-Cluster CLUSTERNAME | Get-VM | % { get-view $_.id } |Where-Object {$_.Guest.ToolsVersionStatus -like "guestToolsNeedUpgrade"} |select name, @{Name="ToolsVersion"; Expression={$_.config.tools.toolsversion}}, @{ Name="ToolStatus"; Expression={$_.Guest.ToolsVersionStatus}}| Sort-Object Name
Perform VMtools Update for all VMs in cluster without reboot
$OutofDateVMs = Get-Cluster CLUSTERNAME | Get-VM | % { get-view $_.id } |Where-Object {$_.Guest.ToolsVersionStatus -like "guestToolsNeedUpgrade"} |select name, @{Name="ToolsVersion"; Expression={$_.config.tools.toolsversion}}, @{ Name="ToolStatus"; Expression={$_.Guest.ToolsVersionStatus}}| Sort-Object Name ForEach ($VM in $OutOfDateVMs){Update-Tools -NoReboot -VM $VM.Name -Verbose}
Automatic VMtools Upgrade/Check at Boot
Check VMs which do or do not automatically check and upgrade tools and boot
Get-Cluster dfw-mx740c-new | Get-VM dfw-m* | Get-View | select name,@{N='ToolsUpgradePolicy';E={$_.Config.Tools.ToolsUpgradePolicy } }
Set VMs that need to have Automatic Tools Upgrade
$ManualUpdateVMs = Get-VM dfw-m* | Get-View | Where-Object {$_.Config.Tools.ToolsUpgradePolicy -like "manual"} | select name,@{N='ToolsUpgradePolicy';E={$_.Config.Tools.ToolsUpgradePolicy } } Foreach ($VM in ($ManualUpdateVMs)) {$VMConfig = Get-View -VIObject $VM.Name $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec $vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo $vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle" $VMConfig.ReconfigVM($vmConfigSpec) }
Get all VMs that have RDM disks in use
Get-VM | Get-HardDisk -DiskType "RawPhysical","RawVirtual" | Select Parent,Name,DiskType,ScsiCanonicalName,DeviceName | fl
Find all VMs with thick provisioned drives
Get-Datastore | Get-VM | Get-HardDisk | Where {$_.storageformat -eq "Thick" } | Select Parent, Name, CapacityGB, storageformat | FT -AutoSize
Find all VMs that have other than vmxnet3 network adapter
Get-VM | Get-NetworkAdapter | Where-object {$_.Type -ne "Vmxnet3"} | foreach ($_) {Write-Host $_.Parent.Name "("$_.Name") type:" $_.Type}
Connect to your vCenter
Connect-VIServer -Server SERVERNAME
Get all VMs that have snapshots
Get-VM | Get-Snapshot | Format-List vm,name,created
Removing a snapshot from a specific VM
Get-Snapshot VMNAME | Remove-Snapshot or Get-Snapshot VMNAME | Remove-Snapshot -Confirm:$false
Check for any attached CDRom drives
Get-VM | where { $_ | get-cddrive | where { $_.ConnectionState.Connected -eq "true" -and $_.ISOPath -like "*.ISO*"} } | select Name, @{Name=".ISO Path";Expression={(Get-CDDrive $_).isopath }}
Disconnect any attached CDRom drives
Get-VM | Get-CDDrive | Where {$_.ISOPath -ne $null} | Set-CDDrive -NoMedia -Confirm:$false
Shutting down or powering up a VM
Get-VM VMNAME | Shutdown-VMGuest Get-VM VMNAME | Start-VM