VMware PowerCLI and ESXi Shell scripts

Small collection of VMware PowerCLI / Powershell scripts and onliners to save time re-typing them:

  1. Get a list of all virtual machines with snapshots and save it to a csv file;
    Get-VM | Get-Snapshot | Select-Object VM, Name, SizeMB, Created, Description | Export-Csv c:snapshots.csv
  2. Get a list of virtual machines, guest IP address, Datacenter, Cluster and Datastore they are running on;
    Get-VM | select @{ Name = "Datacenter"; Expression = {Get-Datacenter -VM $_ }},`
                    @{ Name = "Cluster"; Expression = {Get-Cluster -VM $_ }},`
                    @{ Name = "IPAddress"; Expression = { $_.Guest.IPAddress }},`
                    NumCPU, MemoryMb, ResourcePool, ProvisionedSpaceGB, UsedSpaceGB,`
                    @{ Name = "Datastore"; Expression = {Get-Datastore -VM $_ }},`
                    PowerState, Notes | Sort Name`
                    | Export-Csv "c:DC1.csv"
  3. Change Port Group on multiple VMs;
     $OldPortGroup = 'OLD_PG'
     $NewPortGroup = 'New_PG'
     Get-VM | Get-NetworkAdapter | where {$_.NetworkName -eq $OldPortGroup} | Set-NetworkAdapter -NetworkName $NewPortGroup -Confirm:$false
  4. Enter/Exit Maintenance Mode;
    To enter Maintenance Mode, at the ESX console type:

    vim-cmd  /hostsvc/maintenance_mode_enter

    To exit Maintenance Mode type:

    vim-cmd /hostsvc/maintenance_mode_exit
  5. Get a list of all virtual machines with a specific vNIC type, export to csv file;
    where {$_.Type -eq 'Vmxnet3'} – all VMs with VMXNET3 network adapter type
    where {$_.Type -eq 'e1000'} – all VMs with Intel E1000 network adapter type
    where {$_.Type -eq 'Flexible'} – all VMs with AMD 79C970 PCnet32 – LANCE NIC network adapter type
    where {$_.Type -ne 'Vmxnet3'} – any nic but VMXNET3

    Get-VM | Get-NetworkAdapter | where {$_.Type -eq 'Vmxnet3'} | foreach ($_) {Write-Host $, $_.Name, $_.Type}
    Get-VM | Get-NetworkAdapter | where {$_.Type -eq 'Vmxnet3'} | Select @{N="VM";E={$}}, Name, Type | Export-Csv -Path c:nic.csv
  6. Find virtual machines with disks that need consolidation;
    Find VMs:
    Get-VM | where {$_.ExtensionData.Runtime.consolidationNeeded} | Select Name
    Find VMs and consolidate all:
    Get-VM | where {$_.ExtensionData.Runtime.consolidationNeeded} | %{$_.consolidatevmdisks_task()}
  7. List vmnic and vmkernel MAC addresses of the host;
    Get-VMHostNetworkAdapter | Select @{Name = "ESXi"; Expression = {$_.VMHost.Name}}, Name, Mac
    ESXi                         Name           Mac
    ----                         ----           ---           vmnic0         00:25:b5:01:01:07           vmnic1         00:25:b5:01:01:06           vmk0           00:25:b5:01:01:07           vmk1           00:50:56:71:1d:b9           vmk2           00:50:56:7c:2a:d6
  8. Stay tuned…

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>




This site uses Akismet to reduce spam. Learn how your comment data is processed.