klee222
Enthusiast
Enthusiast

Need a script to reboot VM if uptime is more than 1 days

Hi, I need a help on rebooting few VM machine by checking thier uptimes if more than 1 days.

I have set a schedule to reboot those machine by 7am but some times one or two machines will fail to do so (Due hung or schedule command fails).

And we cannot reboot for a same machine twice, so i need a script to run by schedule to check if any VM machine uptimes is more than 1 day then need to reboot (Force reboot are accepted)

Can somene help me on this, simple scripting will do.

0 Kudos
53 Replies
klee222
Enthusiast
Enthusiast

$71DE62CD6DD06DA1.png

Hi

Here is the result..

0 Kudos
LucD
Leadership
Leadership

Ok, try this way now

$vms = Get-VM -Name (Get-Content -Path C:\vm.txt)

Get-Stat -Entity $vms -Stat sys.uptime.latest -Realtime -MaxSamples 1 |

Select @{N='VM';E={$_.Entity.Name}},@{N='Uptime';E={[math]::Round((New-Timespan -Seconds $_.Value).TotalHours)}} |

where{$_.Uptime -gt 24} | %{

    Get-VM -Name $_.VM |

    Restart-VMGuest -Confirm:$false

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
klee222
Enthusiast
Enthusiast

$3253A2E0C092EBF7.png

Hi

After hit enter 2-3 times, it stop at here...no movement..?

0 Kudos
LucD
Leadership
Leadership

You forgot the enter the closing curly brace, so the PS engine keeps prompting till the code block is closed.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
klee222
Enthusiast
Enthusiast

$76E532F71B0D2629.png

Here is the result...and the result? i don't know it work or not.

and i have 3 VM, but only 2 is scan ?

0 Kudos
LucD
Leadership
Leadership

No, only 2 seem to have passed the Where-clause.

You should be able to see in the Web Client if the guest OS was restarted.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
klee222
Enthusiast
Enthusiast

Hi, it means it only show the success scanned VM only?

0 Kudos
klee222
Enthusiast
Enthusiast

$518EC65B3354266F.png$790BCB7A9335383.png

Cannot complete operation becasue VMware tools is not running in this virtual machine..?

when first time run is ok, after that it prompt something and i forget what is that  i lick "y"

Then i test run second time for few time that a error like above came out..

Help....

0 Kudos
LucD
Leadership
Leadership

Yes, it only shows the VMs that came through the Where-clause.

If you don't have the VMware Tools installed in those guests, you will have to cold restart the VM.

Use the Restart-VM cmdlet instead of the Restart-VmGuest cmdlet.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
klee222
Enthusiast
Enthusiast

Hi

May i know how do i apply that script ?

I'm not good in this

Thanks you

0 Kudos
RahulVmware1985
Enthusiast
Enthusiast

hope this would help you.

Get-Stat -Entity $vms -Stat sys.uptime.latest -Realtime -MaxSamples 1 |

Select @{N='VM';E={$_.Entity.Name}},@{N='Uptime';E={[math]::Round((New-Timespan -Seconds $_.Value).TotalHours)}} |

where{$_.Uptime -gt 24} | %{

    Get-VM -Name $_.VM |

    Restart-VM -Confirm:$false

}

as per LUC you do a replacement. Hope I am correct

0 Kudos
klee222
Enthusiast
Enthusiast

Hi

$FA9BE121630E365.png

I'm using LucD script and there are two problem with this

1) It restart all VM even the the VM are not uptime more than 1 day

2) For the the VM "XP10" the uptime is more than 1 day but could't reboot.

*There are already have a task schedule which reboot all VM by 7am, but due to some VM are not reboot success (Because it Hung after restart) i need help on this to make sure for those are not reboot successfully it will reboot again)

0 Kudos
klee222
Enthusiast
Enthusiast

Hi LucD

There are two problem with the script

1) It restart all VM even the the VM are not uptime more than 1 day

2) For the the VM "XP10" the uptime is more than 1 day but could't reboot. *There are already have a task schedule which reboot all VM by 7am, but due to some VM are not reboot success (Because it Hung after restart)

*i need help on this to make sure for those are not reboot successfully it will reboot again)

0 Kudos
klee222
Enthusiast
Enthusiast

Hi LucD

Need help..Smiley Sad

0 Kudos