Discussions specific to the VMware vSphere hypervisor
Post Reply
bbricker
Enthusiast
Posts: 49
Liked: 23 times
Joined: Feb 10, 2012 8:43 pm
Contact:

Script request

Post by bbricker » Nov 06, 2015 5:31 pm

This is off-topic but I've run out of Google juice and I'm hoping one of you kind souls can help me. Does anyone have a script (PowerCLI or anything similar) to monitor the power state of a vm and boot it back up if it shuts down?

dellock6
Veeam Software
Posts: 5734
Liked: 1625 times
Joined: Jul 26, 2009 3:39 pm
Full Name: Luca Dell'Oca
Location: Varese, Italy
Contact:

Re: Script request

Post by dellock6 » Nov 07, 2015 5:47 pm

I'm curious why a VM may be found in a shut down state... If someone has shut it down by mistake, maybe it's better to review the permissions to access that VM?
Anyway, Get-VM cmdlet has the powerstate option, you can retrieve the actual state of a given vm with something like:

get-vm -name vm_name | select PowerState

and then with a if cycle, power it on if state is "PoweredDown".

Luca
Luca Dell'Oca
Principal EMEA Cloud Architect @ Veeam Software

@dellock6
https://www.virtualtothecore.com/
vExpert 2011 -> 2019
Veeam VMCE #1

Vitaliy S.
Product Manager
Posts: 22991
Liked: 1556 times
Joined: Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov
Contact:

Re: Script request

Post by Vitaliy S. » Nov 08, 2015 9:23 pm

Additionally, you may want to use a script from scenario #4 from this topic > ONEbelievable tricks: Veeam ONE and Automation Services

bbricker
Enthusiast
Posts: 49
Liked: 23 times
Joined: Feb 10, 2012 8:43 pm
Contact:

Re: Script request

Post by bbricker » Nov 23, 2015 5:34 am

dellock6 wrote:I'm curious why a VM may be found in a shut down state... If someone has shut it down by mistake, maybe it's better to review the permissions to access that VM?
It's a stupid situation but for reasons I won't go into here, it shuts down because of a software "issue" every 7 days. It's a server we adopted from another company and as soon as it's two apps go away, so will it. We found it easy just to restart the vm when it shuts off, which is always at night when no one is using it. Then we get another 7 days until it shuts down again.

This is the PowerCLI script I came up with (can't take credit, a buddy helped me out) and I know it's super basic and could be improved upon. Initially it was set to create a scheduled task to rerun itself every 6 days but in the end was easier just to let the script run 24/7. It just checks the vm's powered state every x seconds and if it's off, turns it back on. It will do, at least until we can retire the problem child server.

Code: Select all

------------------------------------------
add-pssnapin VMware.VimAutomation.Core
$viserver = "vcenter-server-name"
$server = "vm-server-name"
$sleepseconds = 300

Connect-VIServer $viserver -user user -password pass  #this could be changed to be more secure of course

while(1){
$vm=get-vm $server
 If ($vm.PowerState -eq "PoweredOff"){
   Write-Host "Powering on $server"
   start-vm $vm
 }
 $date=Get-date
 Write-Host "$date - $server - " $vm.PowerState
 Start-Sleep -s $sleepseconds
}
------------------------------------------
Anyway, sorry for the off-topic'ness of this post! Thanks for the replies!

veremin
Product Manager
Posts: 16897
Liked: 1437 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Script request

Post by veremin » Nov 23, 2015 9:18 am

Just so you know - instead of using scheduled task, you might create a power state alarm in Veeam ONE (if you happen to have it deployed) and set the power-up script as a post activity for it.

This way, everything will be automated - once Veeam ONE finds out that VM is powered off, it will trigger an alarm, which in its turn will execute the corresponding script.

Anyway, thank you for taking your time and sharing the script with the community; appreciated.

Post Reply

Who is online

Users browsing this forum: Bing [Bot], mingji and 35 guests