I am not a powershell expert but I have to use them occasionally to get work done. The purpose of this page, is to keep those handy powershell scripts in a organized manner. So, when ever we need to use them, they are just a click away
“Most of these one-liners are collected over time. They are downloaded from the internet, or copied from VMTN forum. Before, you run any script, you will have to connect to vCenter Server OR to the ESX/ESXi hosts.”
This will be a living document and I will keep on adding more scripts to the list.
This is the first script that you need to run to connect to vCenter Server OR ESX/ESXi server
connect vi-server "Name of the server/IP Address"
Various one liner scripts
1 List all VMs
2 Get one VM
3 Get VMs that match
4 List VMs in a Datacenter
5 List VMs in a cluster
6 Get 2 CPU or larger VMs
7 Get 2 GB RAM or larger VMs
8 Getting information about what you can do with powershell
9 Save a report of VM CPU/MEM configuration information
10 List VMs with connected CD-ROMs
11 Disconnect all connected CD-ROMs
12 Migrate a template to another host
13 Report on Storage
14 Storage VMotion ALL VMs from one LUN to another (All at the same time) COULD BE BAD
15 Storage VMotion ALL VMs from one LUN to another (One at a time)
16 Set all limited CPU reservations to unlimited
17 Set all limited memory reservations to unlimited
18 Configure a new network on a host
19 Configure a new network on a cluster
20 Generate a diagnostic log bundle
21 List the entries in vmkwarning
22 Find something in a log
23 What logs are available?
24 Find VMs with snapshots (Pretty slow – could be optimized)
25 Find VMs with that are poweredon but not from the entire environment, exclude few clusters
26 Get the number of ESX host’s
Get-VM
Get-VM ubuntu01
Get-VM ubuntu*
Get-Datacenter dc001 | Get-VM
Get-Cluster cluster001 | Get-VM
Get-VM | Where-Object {$_.NumCPU -ge 2}
Get-VM | Where-Object {$_.MemoryMB -ge 2048}
8 Getting information about what you can do with powershell
Get-VICommand<br />Get-Command<br />Get-Help Get-VM<br />Get-Member<br />Get-VM | Get-Member
9 Save a report of VM CPU/MEM configuration information
Get-VM | Select-Object Name, NumCPU, MemoryMB | Export-Csv "VM_CPU_Memory_20100720.csv" -NoTypeInformation
10 List VMs with connected CD-ROMs
Get-VM | Where-Object {$_ | Get-CDDrive | Where-Object {$_.ConnectionState.Connected}}
11 Disconnect all connected CD-ROMs
Get-VM | Get-CDDrive | Where-Object {$_.ConnectionState.Connected} | Set-CDDrive -Connected $false -Confirm:$false
12 Migrate a template to another host
Get-Template ubuntu | Set-Template -ToVM | Move-VM -Destination (Get-VMHost esx001.lab.local) | Set-VM -ToTemplate -Confirm:$false
Get-Datastore | Select-Object Name, FreeSpaceMB, CapacityMB | Export-Csv "Datastores_20100720.csv" -NoTypeInformation
14 Storage VMotion ALL VMs from one LUN to another (All at the same time) COULD BE BAD
Get-Datastore "cla_nonrep_01" | Get-VM | Move-VM -Datastore (Get-Datastore "cla_nonrep_00")
15 Storage VMotion ALL VMs from one LUN to another (One at a time)
Get-Datastore "cla_nonrep_01" | Get-VM | ForEach-Object {Move-VM -VM $_ -Datastore (Get-Datastore "cla_nonrep_00")}
16 Set all limited CPU reservations to unlimted
Get-VM | Get-VMResourceConfiguration | Where-Object { $_.CpuLimitMhz -ne -1} | Set-VMResourceConfiguration -CpuLimitMhz $null
17 Set all limited memory reservations to unlimited
Get-VM | Get-VMResourceConfiguration | Where-Object { $_.MemLimitMB -ne -1} | Set-VMResourceConfiguration -MemLimitMB $null
18 Configure a new network on a host
Get-VirtualSwitch -VMHost esx003.lab.local -Name vSwitch0 | New-VirtualPortGroup -Name "VGT Test" -VLanId 4095
19 Configure a new network on a cluster
Get-VirtualSwitch -VMHost (Get-Cluster cluster001 | Get-VMHost) -Name vSwitch0 | New-VirtualPortGroup -Name "VGT Test" -VLanId 4095
20 Generate a diagnostic log bundle
Get-Log -VMHost esx001.lab.local -Bundle -DestinationPath c:\hostlogs
21 List the entries in vmkwarning
(Get-Log -VMHost esx001.lab.local -Key vmkwarning).Entries
(Get-Log -VMHost esx001.lab.local -Key vmkernel).Entries | Select-String "Error"
Get-LogType -VMHost esx001.lab.local
24 Find VMs with snapshots (Pretty slow – could be optimized)
Get-VM | Where-Object {$_ | Get-Snapshot}
25 Find VMs with that are poweredon but not from the entire environment, exclude few clusters
Get-Cluster | where {"lab1","lab2","lab3"-notcontains$_.Name} | get-vm | where-object {$_.Powerstate -eq"Poweredon"} | measure-object
26 Get the number of ESX host’s in the environment
Get-VMhost | measure-object
Leave a Reply