Script request

VMware specific discussions

Script request

Veeam Logoby bbricker » Fri 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?
bbricker
Enthusiast
 
Posts: 48
Liked: 12 times
Joined: Fri Feb 10, 2012 8:43 pm

Re: Script request

Veeam Logoby dellock6 » Sat 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
EMEA Cloud Architect @ Veeam Software

@dellock6
http://www.virtualtothecore.com
vExpert 2011-2012-2013-2014-2015-2016
Veeam VMCE #1
dellock6
Veeam Software
 
Posts: 5055
Liked: 1336 times
Joined: Sun Jul 26, 2009 3:39 pm
Location: Varese, Italy
Full Name: Luca Dell'Oca

Re: Script request

Veeam Logoby Vitaliy S. » Sun 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
Vitaliy S.
Veeam Software
 
Posts: 19575
Liked: 1104 times
Joined: Mon Mar 30, 2009 9:13 am
Full Name: Vitaliy Safarov

Re: Script request

Veeam Logoby bbricker » Mon 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!
bbricker
Enthusiast
 
Posts: 48
Liked: 12 times
Joined: Fri Feb 10, 2012 8:43 pm

Re: Script request

Veeam Logoby v.Eremin » Mon 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.
v.Eremin
Veeam Software
 
Posts: 13296
Liked: 973 times
Joined: Fri Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin


Return to VMware vSphere



Who is online

Users browsing this forum: No registered users and 14 guests