VMware VSphere Power CLI Basics

In my earlier post for   (https://ervikrant06.wordpress.com/2014/07/22/glimpse-of-power-of-vmware-vsphere-power-cli/) I have shown some very basic usages of VSphere Power CLI. Now extending them one step ahead.

Task 1 : How to Stop the VM from Power CLI

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Stop-VM red-linux1

Confirm
Are you sure you want to perform this action?
Performing operation “Stop-VM” on Target “VM ‘Red-Linux1′”.
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is “Y”): Y

Name PowerState Num CPUs MemoryGB
—- ———- ——– ——–
Red-Linux1 PoweredOff 1 1.000

Task 2 : Taking the snapshots of VM named the snapshot as Beforeactivity

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-Vm red-linux1 | New-Snapshot -name Beforeactivity

Name Description PowerState
—- ———– ———-
Beforeactivity PoweredOff

Task 3 : Now if want to list the host present in data center.

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-Datacenter DC1 | Get-VMHost

Name ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz MemoryUsageGB MemoryTotalGB Version
—- ————— ———- —— ———– ———– ————- ————- ——-
192.168.111.132 Connected PoweredOn 2 193 6800 1.245 4.000 5.5.0
192.168.111.133 Connected PoweredOn 2 293 6800 1.554 4.000 5.5.0

If we want to select the first object only we can use the below in addition.

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-Datacenter DC1 | Get-VMHost | Select-Object -first 1

Name ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz MemoryUsageGB MemoryTotalGB Version
—- ————— ———- —— ———– ———– ————- ————- ——-
192.168.111.132 Connected PoweredOn 2 193 6800 1.245 4.000 5.5.0

If we want to select the particular column only use the below command.

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-Datacenter DC1 | Get-VMHost | Select-Object -first 1 | Format-Table Name, PowerState

Name PowerState
—- ———-
192.168.111.132 PoweredOn

If above output is getting distorted then use the below command.

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-Datacenter DC1 | Get-VMHost | Select-Object -first 1 | Format-Table Name, PowerState -autosize

Name PowerState
—- ———-
192.168.111.132 PoweredOn

If we are interested in last object instead of first use below command.

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-Datacenter DC1 | Get-VMHost | Select-Object -last 1

Name ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz MemoryUsageGB MemoryTotalGB Version
—- ————— ———- —— ———– ———– ————- ————- ——-
192.168.111.133 Connected PoweredOn 2 232 6800 1.553 4.000 5.5.0

Task 4 : I realize the power of VSphere CLI by combining the commands using Symbol |

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-Cluster Shiv | Get-VMHost | Get-VM

Name PowerState Num CPUs MemoryGB
—- ———- ——– ——–
Red-Linux2 PoweredOff 1 2.000
ttylinux-11.1-133 PoweredOff 1 0.016
Red-Linux1 PoweredOn 1 1.000
ttylinux-11.1-133 PoweredOff 1 0.016

Again if you want to skep first two entries use below command.

PS C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-Cluster Shiv | Get-VMHost | Get-VM | Select-Object -Skip 2

Name PowerState Num CPUs MemoryGB
—- ———- ——– ——–
Red-Linux1 PoweredOn 1 1.000
ttylinux-11.1-133 PoweredOff 1 0.016

I guess now I should move towards practical scripts by combining power of one liners. Very soon will come up with article on that.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s