ServersAudit all Windows 2000 Services using WMI

Audit all Windows 2000 Services using WMI




by John Loomes

This script takes a text file as input (you
could easily modify this to be a query, or members of a
container…) It then uses WMI to walk through all the services
running on each machine in the list, and pipes the results to a text
file. This allows you to view exactly what you have running on all
your servers, and could be used as the basis of a maintenence
script, that checks the status of various services, and takes
appropriate actions based on what it finds. e.g. attempts tp start
critical services if they are found to be currently stopped.

This script takes a text file as input, it then uses WMI to walk through all the services
running on each machine in the list, and pipes the results to a text
file.

‘ Initialise Error Trapping
On Error Resume Next


Initialize variables
Dim strServerName(1000)
Dim ErrMsg
Dim
strServer
Dim Result
Dim strInputFile
Dim
LogFile

Set objFS =
CreateObject(“Scripting.FileSystemObject”)

‘ Get Name of
Input File and Check to see if its valid
strInputFile =
InputBox(“Enter name of file containing servers (Including full
path)”,,”h:jon.txt”)

Set ServerList = objFS.OpenTextFile
(strInputFile)

If strInputFile = “”
Then
    MsgBox (“Operation Cancelled, no input
file supplied”)
    Wscript.Quit(1)
ElseIf Err
Then
    ErrMsg =
AdsiErr(strInputFile)
    MsgBox (“Error: “&
ErrMsg)
    Wscript.Quit(1)
End
if   

‘ Get Name of Log File and Check to see
if its valid and Writable
LogFile = InputBox(“Enter name of Log
File (Including full path)”,,”c:jon.txt”)

If LogFile = “”
Then
    MsgBox (“Operation Cancelled, no log file
supplied”)
    Wscript.Quit(1)
End
if   
‘ErrMsg = “Logging Started”
‘Result =
WriteLog(,LogFile,ErrMsg)
‘If Err Then
‘   
ErrMsg = AdsiErr(LogFile)
‘    MsgBox (“Error: ”
& ErrMsg)
‘    Wscript.Quit(1)
‘End
if

‘ Parse Input File
‘ List Services on each
machine
do while ServerList.AtEndOfStream
True
   
   
strServerName(xCounter) = ServerList.ReadLine
   

    If not Isblank(strServerName(xCounter))
then
        StrServer =
StrServerName(xCounter)
       
‘ List Services on Current Machine and write to
log
        Result =
ListServices(StrServer,LogFile)
           

        ‘ If not successfully
then try to find out
why
        ‘If Err Then

           
‘ErrMsg =
AdsiErr(strServerName(xCounter))
       
‘else
           
‘ErrMsg = strServerName(xCounter) & ” has been updated
successfully”
        ‘end
if
               

        xCounter = xCounter +
1
   
    End
if
       

loop

Set objFS = Nothing
Set objGroup =
Nothing

‘ ***********************
‘ Functions and Subs
here
‘ ***********************

‘ Trims leading and
trailing spaces
Function IsBlank(strInput)
   

    IsBlank = not
CBool(Len(trim(strInput)))

End Function

‘ List
Services
Function
ListServices(StrNextServer,strLogFile)

    Dim
strTextStream

    Set strTextStream =
objFS.OpenTextFile(strLogFile, 8, true)
   
strTextStream.WriteLine(“——————————————————————————–“)
   
strTextStream.WriteLine(“Services on:    ” &
StrNextServer)
    strTextStream.WriteLine(“Time:
” & Time)
    strTextStream.WriteLine(“Date: ”
& Date)
   
strTextStream.WriteLine(“——————————————————————————–“)
   
strTextStream.WriteLine(“Service Description    
Executable    Status    State
    StartMode    Start
Name”)
   
strTextStream.WriteLine(“——————————————————————————–“)
   

    for each Service in _
   
GetObject(“winmgmts:{impersonationLevel=impersonate}!//” &
StrNextServer).InstancesOf
(“win32_service”)
        ‘
Log Results
        ‘Set objFS
=
CreateObject(“Scripting.FileSystemObject”)
       
strTextStream.WriteLine(Service.Description &
”    ” & Service.PathName & ”
    ” & Service.Status & ”   
” & Service.State & ”    ” &
Service.StartMode & ”     ” &
Service.StartName)

   
next
    strTextStream.Close
   

End Function

‘ Attempt to Trap Errors and return a
message to the log
‘ If Error is Fatal or Unknown then
Quit
Function AdsiErr(ServerName)

Dim e

If
Err.Number = &H80070562 Then
AdsiErr = ServerName & ” has
already been updated.”
ElseIf Err.Number = &H80070005
Then
    AdsiErr = “Access Denied to ” &
ServerName
ElseIf Err.Number = &H1A8
Then
        AdsiErr =
“Couldnt Connect to ” & ServerName
ElseIf Err.Number =
&H800708B2 Then
       
AdsiErr = ServerName & ” is a Domain Controller, cant
update”
    ElseIf Err.Number = &H8007056B
Then
        AdsiErr = “Group
” & ServerName & ” Doesnt Exist”
ElseIf Err.Number = 53
Then
        AdsiErr = “File ”
& ServerName & ” Doesnt Exist”
    ElseIf
Err.Number = 70 Then
       
AdsiErr = “Cant Write to ” &
ServerName
        MsgBox
AdsiErr
       
Wscript.Quit(1)
Else
‘ If error isnt one we expect, flag this
up in a box
e = Hex(Err.Number)
AdsiErr = “Unexpected Error on
” & ServerName
Msgbox (AdsiErr & ” :” &
Err.Number)
End If
   
End
Function

Please note that this is
provided ‘as is’ with no warranties i.e. dont blame me if it all
goes wrong!

Latest Posts

Related Stories