PowerCLI

The Way To PowerCLI : Starting and stopping VMs

In this post we will continue our series ” The Way To PowerCLI ” and today we will talk about starting and stopping VMs

With PowerCLI you can manage and automate your VMware infrastructure from the command line. After you connect to vCenter

start with starting VMs

You can use the complete name of the VM, or you can use a wildcard or use the cmdlet in a pipe. so the cmdlet for starting VM

Start-VM

you can start VM with complete name

Start-VM -VM Test01

2017-09-24_11-43-20

or from pipe input

Get-VM -Name Test02 | Start-VM

2017-09-24_11-44-27

Starting VMs with wildcard

below table show different option

Wildcard character Description Example Matches Does not match
* Matches zero or more characters, starting at the specified position a* A, ag, Apple
? Matches any character at the specified position ?n An, in, on ran
[ ] Matches a range of characters [a-l]ook book, cook, look took
[ ] Matches the specified characters [bc]ook book, cook look

so in first case if we use * it will start all the VMs “test01 and test02”

Start-VM -VM Test*

2017-09-24_12-05-16

second case it will match any character instead of the “?”

Start-VM -VM Test0?

2017-09-24_12-48-03

this time test01 and test02 will start

Start-VM -VM Test0[12]

2017-09-24_12-25-25

you can get more info about Start-VM using Help

2017-09-24_12-55-35

now lest move to Stopping VMs 

to stop VM gracefully  VMware tools must be installed on the VM and you will use command

Shutdown-VMGuest -VM "VM Name"

If you try to stop VMs that aren’t running you get errors and your commands don’t continue, so to check the power state of the VMs

Get-VM -name test* | where {$_.PowerState -eq "PoweredOn"}

2017-09-24_13-05-03

then you can use

Shutdown-VMGuest -VM test*

2017-09-24_13-08-53

if you are not sure what will happen if you run this command  you can add “-WhatIf” at end of the cmdlet and powercli will simulate what will happen if you run this cmdlet

Shutdown-VMGuest -VM test* -WhatIf

2017-09-24_13-10-12

Stopping VMs without VMware tools

to stop VM that don’t have VMware tools use cmdlet Stop-VM

Stop-VM -VM tlab*

2017-09-24_13-21-03

this will force shutdown the VM without saving anything

 

Resources :

Supporting Wildcard Characters in Cmdlet Parameters

” The way to VMware PowerCLI” Series Posts :

 

2 thoughts on “The Way To PowerCLI : Starting and stopping VMs

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