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
LucD
Leadership
Leadership

To clarify, are you restarting the VM or restarting the guest OS ?


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

0 Kudos
klee222
Enthusiast
Enthusiast

I'm restarting the VM, mean i have build few VM in esx host and i would like to restart it, like shutdown and reboot again

0 Kudos
LucD
Leadership
Leadership

The following will show you total hours of uptime for a VM.

It should be straightforward to decide on that value if a machine needs to be restarted

$vms = Get-VM

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)}}


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

0 Kudos
klee222
Enthusiast
Enthusiast

If i need to restart if uptime id more than 1 days? any script can help so can work together?

Mean if found any vm uptime is more than 1 day, it will reboot the vm automatically

0 Kudos
Craig_Baltzer
Expert
Expert

Get-VM | Where-Object {($_.ExtensionData.RunTime.BootTime -lt (get-date).AddDays(-1)) -and ($_.PowerState -eq "PoweredOn")} | `

   Restart-VMGuest -Confirm:$False

This assumes that all of the VMs have VMware Tools installed...

0 Kudos
LucD
Leadership
Leadership

You could do something like this

$vms = Get-VM

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} |

Restart-VMGuest -VM $_.VM -Confirm:$false


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

0 Kudos
klee222
Enthusiast
Enthusiast

Hi, thanks but may i know which part need to fill up for server name ?

and other place need to be consider ?

Thanks you

0 Kudos
klee222
Enthusiast
Enthusiast

Hi, Thanks for the script but can help explain which part is for server name?

I'm not good in programming so hope can get some explanation, like server name edit and how it work?

Thousand thanks

0 Kudos
LucD
Leadership
Leadership

With the current line

$vms = Get-VM

the script will look at all VMs.

If you want to go for 1 specific VM, you could do

$vms = Get-VM -Name MyServer

You can also select all VMs whose names starts with MyVM by adding an asterisk.

$vms = Get-VM -Name MyServer*

Or do you mean something completely different ?


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

0 Kudos
klee222
Enthusiast
Enthusiast

Hi,

Let say i have 20+ VM with name HKWBPO1 util HKBPO20,

How do i run the script?

Thanks you

0 Kudos
LucD
Leadership
Leadership

You could try something like this

%$names = 1..20 | %{

    "HKWBP{0:d2}" -f $_

}

$vms = Get-VM -Name $names

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} |

    Restart-VMGuest -VM $_.VM -Confirm:$false   

}


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

0 Kudos
klee222
Enthusiast
Enthusiast

Hi,

For the name i just make assume only , i have few different VM name so how can i do that uptimme cheking and reboot for different VM name.

Thanks you

0 Kudos
LucD
Leadership
Leadership

You'll have to have the VM names somewhere.

Or you can select them with a name mask, generate a list of names (like the previous example) or have them in a file somewhere.

So, how do you select the VMs you want to check and eventually reboot ?


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

0 Kudos
klee222
Enthusiast
Enthusiast

$3B6763FB7BB7F528.jpg

Here is the one structure of the VM that i had, those VM are setup on one of the cluster here (assume the 4th cluster "STDC"). so base on this structure i need to check the selected VM uptime and reboot is uptime is more than 1 days (VM have different name).

Please help

0 Kudos
LucD
Leadership
Leadership

To select all the VMs in that cluster you can do

$vms = Get-Cluster -Name STDC | Get-VM


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

0 Kudos
klee222
Enthusiast
Enthusiast

Hi,

is not all the VM in the cluster, only few selected VM.

Any way to check for selected VM for uptime and reboot?

Thanks you

0 Kudos
LucD
Leadership
Leadership

Yes, that is possible, but you will have to tell how you select those VMs.


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

0 Kudos
klee222
Enthusiast
Enthusiast

Hi

I'm not sure about it, but i saw in here someone can do select from a "text" file where all the vm list in there.

From there the program can scan according to the list, that whats i know.

Or you con provide more efficient way ?

i have few VM with different name in two different cluster, i'm not good in programming so need help on this.

Thanks you

0 Kudos
LucD
Leadership
Leadership

Yes, you can get the VMs via the names in a TXT file.

Assume you have a VMname on each line in a TXT file, then you could do

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


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

0 Kudos