-
- Enthusiast
- Posts: 54
- Liked: 27 times
- Joined: Feb 10, 2012 8:43 pm
- Contact:
Script request
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?
-
- VeeaMVP
- Posts: 6165
- Liked: 1971 times
- Joined: Jul 26, 2009 3:39 pm
- Full Name: Luca Dell'Oca
- Location: Varese, Italy
- Contact:
Re: Script request
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
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 -> 2022
Veeam VMCE #1
Principal EMEA Cloud Architect @ Veeam Software
@dellock6
https://www.virtualtothecore.com/
vExpert 2011 -> 2022
Veeam VMCE #1
-
- VP, Product Management
- Posts: 27368
- Liked: 2799 times
- Joined: Mar 30, 2009 9:13 am
- Full Name: Vitaliy Safarov
- Contact:
Re: Script request
Additionally, you may want to use a script from scenario #4 from this topic > ONEbelievable tricks: Veeam ONE and Automation Services
-
- Enthusiast
- Posts: 54
- Liked: 27 times
- Joined: Feb 10, 2012 8:43 pm
- Contact:
Re: Script request
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.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?
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
}
------------------------------------------
-
- Product Manager
- Posts: 20389
- Liked: 2298 times
- Joined: Oct 26, 2012 3:28 pm
- Full Name: Vladimir Eremin
- Contact:
Re: Script request
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.
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.
Who is online
Users browsing this forum: No registered users and 86 guests