Link to home
Start Free TrialLog in
Avatar of BravehearT-1326
BravehearT-1326Flag for United Kingdom of Great Britain and Northern Ireland

asked on

Need Script to check multiple servers to see if a service is installed or not

Hi there

I'd like a script to do the following:

1 - Build a list of servers from the 2 domains we have
2 - Check each of the servers that the first part brought back to see if a Windows service is installed or not.

Ideally the script would output in the formatof:

ServerA - Installed
ServerB - Installed
ServerC - Not Installed.

I am a total neewbie at scripting and havent got a clue where  to start.

Thanks for assistance.
Avatar of BravehearT-1326
BravehearT-1326
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

I understand I could just run the following for each of the servers concerned as well....

sc \\servernamehere query "ServiceNameGoesHere" | find /i "running"
Managed to get a script generator that allows me to view ALL the services for 1 particular server but dont know how easy it is to customize it for my purposes.

I obviously need some sort of array / input file for the computer name and the ability to report on 1 particular service name.
On Error Resume Next
 
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
 
arrComputers = Array("ServernameHERE")
For Each strComputer In arrComputers
   WScript.Echo
   WScript.Echo "=========================================="
   WScript.Echo "Computer: " & strComputer
   WScript.Echo "=========================================="
 
   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)
 
   For Each objItem In colItems
       WScript.Echo "DisplayName: " & objItem.DisplayName
       WScript.Echo "ServiceStarted: " & objItem.Started
      WScript.Echo "ServiceStartMode: " & objItem.StartMode
      WScript.Echo "ServiceAccount: " & objItem.StartName
      WScript.Echo "State: " & objItem.State
      WScript.Echo "Status: " & objItem.Status
        WScript.Echo
   Next
Next
 
 
Function WMIDateStringToDate(dtmDate)
WScript.Echo dtm: 
	WMIDateStringToDate = CDate(Mid(dtmDate, 5, 2) & "/" & _
	Mid(dtmDate, 7, 2) & "/" & Left(dtmDate, 4) _
	& " " & Mid (dtmDate, 9, 2) & ":" & Mid(dtmDate, 11, 2) & ":" & Mid(dtmDate,13, 2))
End Function

Open in new window

Avatar of Qlemo
Seems to much of ado for your purpose, that vbs script. Following cmd code could do much simpler.

It checks whether the service is running, as your sc example states this; however, your question asks for Install state. That is part two, then. Just choose which one you want.

@echo off
setlocal enabledelayedexpansion
 
REM generate list of (running) computers
(for /F "skip=3" %%C in ('net use /domain:yourdomain1 ^| find "\\") do echo %%C) > pc.lst
(for /F "skip=3" %%C in ('net use /domain:yourdomain2 ^| find "\\") do echo %%C) >> pc.lst
 
REM asking them for the run state of specific service
for /F %%C in (pc.lst) do (
  set running=not
  sc %%C query "ServiceName" | findstr /L running >nul && set running=
  set pc=%%C
  set pc=!pc:\=!
  echo !pc! - !running! running
)
 
REM asking for install state
for /F %%C in (pc.lst) do (
  set installed=
  sc %%C query "ServiceName" | findstr /C:"FAILED 1060" >nul && set installed=not
  set pc=%%C
  set pc=!pc:\=!
  echo !pc! - !installed! installed
)
 
del pc.lst >nul

Open in new window

This would be the slight adustment to your script - including an output report:
ServerList = "C:\Servers.txt"
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOut : Set objOut = objFSO.CreateTextFile("C:\ServiceQuery.txt")
 
'On Error Resume Next
 
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
 
arrComputers = Split(objFSO.OpenTextFile(ServerList).ReadAll, vbNewLine) 'Array("ServernameHERE")
 
For Each strComputer In arrComputers
  With objOut
    .WriteLine "The following services are installed on " & strComputer
    .WriteLine 
   
   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
 
   For Each objItem In colItems
       objOut.WriteLine vbTab & "DisplayName: " & objItem.DisplayName
       objOut.WriteLine vbTab & "ServiceStarted: " & objItem.Started
       objOut.WriteLine vbTab & "ServiceStartMode: " & objItem.StartMode
       objOut.WriteLine vbTab & "ServiceAccount: " & objItem.StartName
       objOut.WriteLine vbTab & "State: " & objItem.State
       objOut.WriteLine vbTab & "Status: " & objItem.Status
       objOut.WriteLine 
   Next
   objOut.WriteLine "================"
Next
 
objOut.Close

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for taking the time / effort to assist with this script issue.  It is much appreciated.