VMware Cloud Community
essisim
Contributor
Contributor
Jump to solution

Simple script help (script noob)

Hi,

could anyone help me write a script for doing a simple task?
I would like the scipt to:

Shutdown Machine A

Check when Machine A is powered off, then power on Machine B

Thanks

Reply
0 Kudos
1 Solution

Accepted Solutions
vHaridas
Expert
Expert
Jump to solution

don't use stop-vm -kill. because of this HA is restarting first VM.

Use shutdown-VMGuest command to shutdown Guest OS of VM, this will need VM tools to be running.

Also you need to wait for couple of minutes before you for next command because, Shutdown of First VM will take some time.

Please consider awarding points for "Correct" or "Helpful" replies. Thanks....!!! https://vprhlabs.blogspot.in/

View solution in original post

Reply
0 Kudos
4 Replies
Marmotte94
Enthusiast
Enthusiast
Jump to solution

hi,

You can check this script, it works in my environment.

Connect-VIServer <YourvCenter>

$GetPower= Get-VM <vmA>

if ($GetPower.powerstate -eq "PoweredOn") {

Get-VM <vmA> | Where-Object {$_.PowerState -eq "PoweredOn"} | Stop-VM -kill -confirm:$false

Get-VM <vmB> | Start-VM

}

else {

Get-VM <vmB> | Start-VM

}

Regards,

Please, visit my blog http://www.purplescreen.eu/
Reply
0 Kudos
essisim
Contributor
Contributor
Jump to solution

Thanks for the script.

It almost works perfectly, however:

It shuts down "testvm"

starts up "testvm2"

and starts up "testvm" right afterwards.

$GetPower= Get-VM testvm

if ($GetPower.powerstate -eq "PoweredOn") {

Get-VM testvm | Where-Object {$_.PowerState -eq "PoweredOn"} | Stop-VM -kill -confirm:$false

Get-VM testvm2 | Start-VM

}

else {

Get-VM testvm2 | Start-VM

From the events log on "testvm", it says that vSphere HA restarted the virtual machine...not sure why it does this?

any tips?

Reply
0 Kudos
vHaridas
Expert
Expert
Jump to solution

don't use stop-vm -kill. because of this HA is restarting first VM.

Use shutdown-VMGuest command to shutdown Guest OS of VM, this will need VM tools to be running.

Also you need to wait for couple of minutes before you for next command because, Shutdown of First VM will take some time.

Please consider awarding points for "Correct" or "Helpful" replies. Thanks....!!! https://vprhlabs.blogspot.in/
Reply
0 Kudos
Marmotte94
Enthusiast
Enthusiast
Jump to solution

Or change your settings for this VM in your cluster HA "Virtual Machine Options" set "Disable".

Do not change setting for your Cluster.

Regards,

Please, visit my blog http://www.purplescreen.eu/
Reply
0 Kudos