A few basic PowerCLI oneliners.

Some students in my classes have been recently asking me for any good PowerCLI oneliners during my ICM classes.

Here’s a list I typically share out to my students. If you’ve got any to add, feel free to leave them in the comments section. It’s nice to get a list of these things. I also recommend the PowerCLI lab over at vNugglets to anybody getting started. Matt and Allen wrote the lab for last years Demo Days VMUG event.

Do VMs have snapshots?

foreach ($snap in (Get-VM | Get-Snapshot | Where-object {$_.Created -lt ((Get-Date).AddDays(0))})){ $snap | select VM, Name, Description, SizeMB, Created }

Rescan HBA Adapters.

foreach ($esx in Get-VMhost -Location ClusterName | sort Name) { $esx | Get-VMHostStorage -RescanAllHBA -rescanVMFS -refresh }

Add Port Group “VLAN 500” with VLAN tag 500 to vSwitch1 on all hosts in a Cluster

foreach ($esx in Get-VMHost -Location ClusterName | Sort-Object Name) { $esx | Get-VirtualSwitch -Name vSwitch1 | New-VirtualPortGroup -Name “VLAN500” -VlanId 500 }

vMotion and Storage vMotion

Get VM Information, Cluster, Host, Datastore

Get-VM | Select Name, @{N=”Cluster”;E={Get-Cluster -VM $_}},@{N=”ESX Host”;E={Get-VMHost -VM $_}},@{N=”Datastore”;E={Get-Datastore -VM $_}}

Actually vMotion

Move-VM vm-name -Destination (Get-VMHost esxi-hostname)

Actually Storage vMotion

Get-VM “vm-name” | Move-VM -datastore (Get-datastore datastore-name)

Search for things in Inventory. (Look for VMs that have an ‘v’ in them.

Get-VM | Where-Object {$_.name -match ‘v’}

List VMs with less than 1024MB of Memory

Get-VM | Where-Object {$_.MemoryMB -lt ‘1024’}

See if any of our datastores have less than a certain amount of free space. First we’ll do 100,000MB. Then we’ll search for 3,000MB

Get-Datastore | Where-Object {$_.freespaceMB -lt 100000}

Get-Datastore | Where-Object {$_.freespaceMB -lt 3000}

Output a list

get-vm | select name, numcpu, memoryMB, @{n=”diskKB”; e={($_ | get-harddisk).capacityKB}} | ConvertTo-Html | Out-File c:vms.html

This entry was posted in Scripting. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *