none
Powershell Script to Restart Computer After 3 days RRS feed

  • Question

  • Hi guys,

    I need a powershell script to restart computers that have been up for over three days forcefully.

    I have the Vbs script below; however when I apply the script as a policy, the computers are in a continuous restart loop.

    Kindly help to fix

    'Restart the local device if it has not been shutdown in more than X Hours'

    strComputer = "."
    X = 72

    set objWMIDateTime = CreateObject("WbemScripting.SWbemDateTime")
    set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    set colOS = objWMI.InstancesOf("Win32_OperatingSystem")
    for each objOS in colOS

    objWMIDateTime.Value = objOS.LastBootUpTime

    'Check the device uptime in hours
    UptimeHours = TimeSpan(objWMIDateTime.GetVarDate,Now)

    Next

    'Check to see if > than X hours and restart if true
    If UptimeHours > X Then
    MsgBox "Your computer has not been restarted in over " & X & " hours, to ensure best performace your computer will now automatically restart", vbExclamation + vbOKOnly, "Sahara ITHelpdesk"
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate,(Shutdown)}!\\" & _
            strComputer & "\root\cimv2")

    Set colOperatingSystems = objWMIService.ExecQuery _
        ("Select * from Win32_OperatingSystem")

    For Each objOperatingSystem in colOperatingSystems
        objOperatingSystem.Reboot()
    Next
    Else
    End if

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    Function TimeSpan(dt1, dt2) 
    ' Function to display the difference between
    ' 2 dates in hh:mm:ss format
    If (isDate(dt1) And IsDate(dt2)) = false Then 
    TimeSpan = "00:00:00" 
    Exit Function 
            End If 
     
            seconds = Abs(DateDiff("S", dt1, dt2)) 
            minutes = seconds \ 60 
            hours = minutes \ 60 
            minutes = minutes mod 60 
            seconds = seconds mod 60 
     
            if len(hours) = 1 then hours = "0" & hours 
     
            TimeSpan = hours
            
    End Function

    Thursday, January 2, 2020 8:54 PM

All replies

  • You should not be using VBScript as it is obsolete. PowerShell is much easier and is more powerful.

    $os = Get-CimInstance Win32_OperatingSystem
    if(([datetime]::Now - $os.LastBootUpTime).Hours -gt 72){
        Restart-Computer -Force
    }else{
        # no reboot needed
    }


    \_(ツ)_/

    Thursday, January 2, 2020 9:11 PM
  • If you want to prompt just add a prompt:

    Add-Type -AssemblyName System.Windows.Forms
    $os = Get-CimInstance Win32_OperatingSystem
    if(([datetime]::Now - $os.LastBootUpTime).Hours -gt 72){
        if('Yes' -eq [System.Windows.Forms.MessageBox]::Show('needs rebooting','Reboot?','YesNo')){
            Restart-Computer -Force
        }
    }else{
        # no reboot needed
    }
    


    \_(ツ)_/

    Thursday, January 2, 2020 9:15 PM
  • Thanks for your response.
    I have deployed it using the powershell script has written above as a logon script.
    However it is not taking effect on PCs that are over 3days old.

    What could be wrong?

    Monday, January 6, 2020 1:48 PM
  • Only an administrator can run that script.


    \_(ツ)_/

    Monday, January 6, 2020 2:00 PM