Four PowerCLI One-Liners That Saved The World!

Ok, So maybe they didn’t save the world, but the sure saved my weekend. Rather than spend a lot of time on the intro, let’s dive right in. These will be in Problem/PowerShell/Output format.

The Problem:

More spaghetti environments, this time we need to count some resources. The first counts CPU, the second Memory. Yes there are pretty reports for this, but when you need the answer, you need it now.

The PowerShell:

get-datacenter "SomeDC" | get-cluster "clustername" |get-vm | select numcpu | %{ $cpus += $_.numcpu }
get-datacenter "SomeDC" | get-vm | select memorymb | %{ $memorymb += $_.memorymb }

The Output:

$cpus
60

$memorymb
401388

The Problem:

Need to rapidly identify what Datastore is associated with a VM or VMs. This is helpful to get a handle on the spaghetti that an poorly maintained environment can become over time.

The PowerShell:

get-datacenter | get-vm | %{ $_.Name; ($_ | get-datastore | select Name).Name }

The Output:

VM1.professionalvmware.com
VM Storage

The Problem:

Need to rapidly identify what the condition is in. Because perusing the entire VI for one server, and then clicking “Configuration > Server Health” is too much work.

The PowerShell:

get-vmhost -name "servername" | get-view | %{$_.Runtime.HealthSystemRuntime}  | %{ $_.HardwareStatusInfo.StorageStatusInfo } | where { $_.Name -like "Battery*" } | %{ $_.Status}

The Output:

Key             : Green
Label           : Green
Summary         : Physical element is functioning as expected
DynamicType     :
DynamicProperty :

The Problem:

Cold migrating A LOT of VMs. Note: This could be changed in a few ways that would allow much cooler things, but this is what I needed at the time and how I got there.

The PowerShell:

import-csv c:\maint.csv | %{ get-vm -name $_.ServerName -Location (get-datacenter "SomeDatacenter") | move-vm -Datastore (get-datastore -name "SomeDataStore") -RunAsync -Confirm:$false }

Example CSV:

ServerName
vm1.professionalvmware.com
vm5.professionalvmware.com
vm6.professionalvmware.com
vm7.professionalvmware.com

The Output:

Id              :
IsCancelable    : False
IsComplete      : False
Result          :
Name            : RelocateVM_Task
Description     : Relocate Virtual Machine storage
State           : Running
PercentComplete : 0
StartTime       : 9/12/2009 7:14:17 PM
FinishTime      :
ObjectId        :

One thought on “Four PowerCLI One-Liners That Saved The World!”

  • Pingback: Tweets that mention Four PowerCLI One-Liners That Saved The World! -- Topsy.com

Comments are closed.