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 »

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: 5918
Liked: 1739 times
Joined: Jul 26, 2009 3:39 pm
Full Name: Luca Dell'Oca
Location: Varese, Italy
Contact:

Re: Script request

Post by dellock6 »

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 -> 2020
Veeam VMCE #1

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

Re: Script request

Post by Vitaliy S. »

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 »

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: 17777
Liked: 1628 times
Joined: Oct 26, 2012 3:28 pm
Full Name: Vladimir Eremin
Contact:

Re: Script request

Post by veremin »

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: Majestic-12 [Bot] and 19 guests