VMware Cloud Community
glynnd1
Expert
Expert
Jump to solution

Power on/off particular VMs, sample code

I needed a script that would power off or on all the VMs of a particular naming type (eg the name began with the same few letters) in a particular cluster.

eg "power.ps1 SW MI off" Will power off all VMs beginning with MI in cluster(s) beginning with SW.

It does some error checking, ie it won't power on a VM that is already powered on.

I do use it with the server IP, user and password hard coded, dropping the user and password will pop up the expect msgbox.

I've no doubt that someone can take my 40 lines of code and do it in five Smiley Happy

edit:

To power off VMs it uses "Shutdown-VMGuest $VM", this will fail if VMware tools is not running, a check could be added for VMware tools and in it's absents "Stop-VM $VM" be used instead.

0 Kudos
1 Solution

Accepted Solutions
hugopeeters
Hot Shot
Hot Shot
Jump to solution

I'd write things like this into a function and store it in my profile. (As well as Add-PSSnapIn.)

My attempt to do it in five lines: see attachment. Smiley Happy

Hugo

www.peetersonline.nl

View solution in original post

0 Kudos
2 Replies
hugopeeters
Hot Shot
Hot Shot
Jump to solution

I'd write things like this into a function and store it in my profile. (As well as Add-PSSnapIn.)

My attempt to do it in five lines: see attachment. Smiley Happy

Hugo

www.peetersonline.nl

0 Kudos
glynnd1
Expert
Expert
Jump to solution

Thanks Hugo. I knew I was using too many curly braces.

0 Kudos