VBScript to check the Service Status and Startup Type

' Description  : This script will display the Service status and startup type in a Excel file
' Instructions : Create a text file by name servers.txt in C: drive with the server names
'                which u need to check the Service status and startup type. Output will
'                be an excel file which will be saved in C: drive with file name
'                as "Services_<date> <time>.xls"


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 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,1) = "Servername"
objworksheet.cells(1,2) = "Status"
objWorksheet.cells(1,3) = "Startup Type"
objWorksheet.cells(1,3) = "Remarks"
objworksheet.range("A1:D1").font.size= 12
objworksheet.range("A1:D1").font.bold= True
objWorksheet.range("A1:D1").interior.colorindex= 33
objExcel.Columns.Borders.ColorIndex=56

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

x = 2
Do
strComputer = objfile.ReadLine

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colServices = objWMIService.ExecQuery("Select * From Win32_Service")

objworksheet.cells(x,1).value = strComputer

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
    objWorksheet.cells(x, 4) = ""
    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"
 End If

Next
 x = x + 1


Loop Until objfile.AtEndOfStream = True
  

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

objworkbook.SaveAs strSavefile

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

objExcel.Application.Quit

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

' End of script



***Share your comments about the post***

No comments:

Post a Comment