Protected Overrides Sub OnStart(ByVal args() As String)
Dim Timer As New Timers.Timer()
OpenLog()
AddLog("-----------------------------------------")
AddLog(Me.ServiceName & " Started [" & GetVersion() & "]")
AddLog("Log Open")
AddLog("Database Open : " & OpenSQL())
AddHandler Timer.Elapsed, AddressOf OnTimedEvent
Timer.Interval = (GetDataFromXML("/config/chkinterval") * 1000)
Timer.Start()
AddLog("Timer Started : " & (Timer.Interval / 1000) & " second loop interval")
End Sub
Private Sub OnTimedEvent(ByVal source As Object, ByVal e As Timers.ElapsedEventArgs)
Dim oDataReader As SqlDataReader
Dim iCounter As Integer
iCounter = 0
Try
If SQLCheck() Then
Dim oCmd As New SqlCommand("BootGet", SQLConnObj)
oDataReader = oCmd.ExecuteReader()
While oDataReader.Read()
Dim myBootClass As New BootClass()
Dim bootThread As New Thread(AddressOf myBootClass.Reboot)
myBootClass.Server = oDataReader("Server")
myBootClass.Key = oDataReader("PKey")
myBootClass.Notify = oDataReader("Notify")
bootThread.IsBackground = True
bootThread.Name = "thd_" & iCounter.ToString()
bootThread.Start()
iCounter += 1
'ThreadPool.QueueUserWorkItem(AddressOf BootClass.Reboot, "Running") ', oDataReader("Server") & "|" & oDataReader("PKey") & "|" & oDataReader("Notify"))
AddLog("Reboot thread initialized [" & oDataReader("Server") & "-" & bootThread.Name & "]")
End While
oDataReader.Close()
TimeErrCnt = 0
End If
Catch ex As Exception
If oDataReader.IsClosed = False Then
oDataReader.Close()
End If
TimeErrCnt += 1
If TimeErrCnt >= 5 Then
AddLog("Timer Error : " & TimeErrCnt & " : Critical error level exceeded, service shutting down. Error Detail: BootManager:OnTimedEvent")
SendNotify(ex.Message, GetDataFromXML("/config/adminnotify"))
End
End If
Thread.CurrentThread.Sleep(10000)
AddLog("Timer Error : " & TimeErrCnt & " : " & ex.Message & " Error Detail: BootManager:OnTimedEvent")
End Try
End Sub
Public Class BootClass
Public Server As String
Public Key As Integer
Public Notify As String
Public Sub Reboot()
Dim BootErrCnt As Integer = 1
If SQLCheck() Then
Dim Path As New ManagementPath()
Path.Server = Server
Path.NamespacePath = "root\CIMV2"
Dim Scope As New ManagementScope(Path)
Scope.Options.Username = UserName
Scope.Options.Password = Password
Scope.Options.Impersonation = ImpersonationLevel.Impersonate
Scope.Options.EnablePrivileges = True
Do While BootErrCnt <= 5
Try
Scope.Connect()
Exit Do
Catch ex As Exception
If BootErrCnt = 5 Then
If SQLCheck() Then
Dim oFCmd As New SqlCommand("BootLogInsert " & Key & ",'" & ex.Message & "(Error Count:" & BootErrCnt & " : Exiting thread)'", SQLConnObj)
oFCmd.ExecuteNonQuery()
End If
AddLog("Boot Error : " & Server & " : " & BootErrCnt & " : " & ex.Message & " : Exiting thread")
If Not Notify = "" Then
SendNotify(Server & " reboot failure", Notify)
End If
Exit Sub
Else
Thread.CurrentThread.Sleep(10000)
If SQLCheck() Then
Dim oECmd As New SqlCommand("BootLogInsert " & Key & ",'" & ex.Message & "(Error Count:" & BootErrCnt & ")'", SQLConnObj)
oECmd.ExecuteNonQuery()
End If
AddLog("Boot Error : " & Server & " : " & BootErrCnt & " : " & ex.Message)
BootErrCnt += 1
End If
End Try
Loop
Dim Query As New ObjectQuery("SELECT * FROM Win32_OperatingSystem")
Dim Searcher As New ManagementObjectSearcher(Scope, Query)
Dim myObject As New ManagementObject()
Dim InParams As ManagementBaseObject()
For Each myObject In Searcher.Get
myObject.Scope.Options.EnablePrivileges = True
myObject.InvokeMethod("Reboot", InParams)
Next
If SQLCheck() Then
Dim oSCmd As New SqlCommand("BootLogInsert " & Key & ", 'Successfull'", SQLConnObj)
oSCmd.ExecuteNonQuery()
End If
AddLog("Reboot Success : " & Server)
If Not Notify = "" Then
SendNotify(Server & " successfully rebooted", Notify)
End If
End If
End Sub
End Class