VBScript to check Service status stop and start

' Provide server names in servers.txt file and place it in C: drive. Output will be saved as                              'Services_<Service Name> <date> <time>.xls in C: drive.
' Note: you need to run this script on machine where excel is installed and access to ' other servers


Const Reading = 1

Dim objExcel, objWorksheet, objWorkbook, objRange
Dim objNtpad, ofjfile, inSvr, strSavefile, strDate, strTime
Dim strComputer

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()
Set objWorksheet = objWorkbook.Worksheets(1)

Set WSHShell = WScript.CreateObject("WScript.Shell")
Set objLocator = CreateObject("WbemScripting.SWbemLocator")

Set objNtpad = CreateObject("Scripting.FileSystemObject")
Set objFile = objNtpad.OpenTextFile("C:\servers.txt", Reading)

inSvr = InputBox("Please enter name of the Service: Note: Enter the Exact Service name its Case sensitive", _
                  "Create File")

On Error Resume Next
Set objexcel=CreateObject("Excel.application")
If(number <> 0) Then
  On Error Goto 0
  WScript.Echo "Excel application not found"
  WScript.Quit
End If

objWorksheet.cells(1,4) = "Service Entered: " & inSvr
objworksheet.range("A5").font.size= 10
objWorksheet.cells(3,1) = "Servername"
objWorksheet.cells(3,2) = "Status"
objWorksheet.cells(3,3) = "Startup Type"
objWorksheet.cells(3,4) = "Stopped"
objWorksheet.cells(3,5) = "Started"
objWorksheet.cells(3,6) = "Remarks"
objWorksheet.range("A3:F3").font.size= 10
objworksheet.range("A3:F3").font.bold= True
objWorksheet.range("A3:F3").interior.colorindex= 33
objExcel.Columns.Borders.ColorIndex=56

strDate= Replace(Date,"/","-")
strTime= Replace(Time,":","-")
strSavefile = "C:\Services_" & inSvr & " " & strDate & " " & strTime & ".xls"

x = 4

Do
i = x
strComputer = objfile.ReadLine
objworksheet.cells(x,1).value = strComputer
On Error Resume Next
    Err.Clear
Set objWMIService = objLocator.ConnectServer(strComputer, "root\cimv2")
    If Err.Number <> 0 Then
 '    objWorksheet.cells(x, 4) = "Server is offline"
     Call failed
    Else
'     objWorksheet.cells(x, 4) = "Server is online"
     Call success
 
    End If

 x = x + 1
Loop Until objfile.AtEndOfStream = True

Sub notfound()
objWorksheet.cells(i,4) = "Service not Found"
End Sub

Sub failed()
     objWorksheet.cells(i, 4) = "Server is offline/No Login Access/No DNS record"
End Sub

Sub success()
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServices = objWMIService.ExecQuery("Select * From Win32_Service")

For Each objService in colServices
 If objservice.name = inSvr Then
   If objservice.state = "Running" Then
    objworksheet.Cells(x, 2) = "Running"
    objWorksheet.Cells(x, 3) = objservice.startmode
    objservice.stopservice()
    WScript.Sleep 5000
    objWorksheet.Cells(x, 4) = "Yes"
    objservice.startservice()
    WScript.Sleep 5000
    objWorksheet.Cells(x, 5) = "Yes"
    objWorksheet.cells(x, 6) = ""
    Exit For
   Else
    objworksheet.Cells(x, 2) = "Not Running"
    objWorksheet.Cells(x, 3) = objservice.startmode
    objWorksheet.cells(x, 4) = ""
  Exit For
   End If
 Else
'   objWorksheet.cells(x, 4) = "Service not found"
Call notfound
 End If
Next
End Sub
 

Set objrange = objWorksheet.Usedrange
objrange.entirecolumn.autofit()

objworkbook.SaveAs strSavefile

WScript.Echo "Excel file has been saved in C:\Services_" & inSvr & " " & strDate & " " & strTime & ".xls"

objExcel.Application.Quit
objExcel.Quit

Set objExcel = Nothing
Set objworksheet = Nothing
Set objWMIservice= Nothing
Set objrange = Nothing
Set objFile = Nothing
Set objNtpad = Nothing
Set inSvr = Nothing




***Share your comments about this post***

No comments:

Post a Comment