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.
BravehearT-1326Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BravehearT-1326Author Commented:
I understand I could just run the following for each of the servers concerned as well....

sc \\servernamehere query "ServiceNameGoesHere" | find /i "running"
0
BravehearT-1326Author Commented:
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

0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
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

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

sirbountyCommented:
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

0
sirbountyCommented:
You mentioned you only wanted to check 3 services though - this is one method to accomplish that:
ServerList = "C:\Servers.txt"
arrServices = Array("MyService.exe","MyOtherSvc.exe","SomeService.exe")
 
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
 
 
Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim objOut : Set objOut = objFSO.CreateTextFile("C:\ServiceQuery.txt")
 
 
arrComputers = Split(objFSO.OpenTextFile(ServerList).ReadAll, vbNewLine) 
 
For Each strComputer In arrComputers
  With objOut
    .WriteLine "Report for " & strComputer
    .WriteLine 
   
   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
 
  For Each strSvc in arrServices
    On Error Resume Next
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Service Where Name ='" & strSvc & "'", "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
  Next
 
  objOut.WriteLine "================"
  objOut.WriteLine
Next
 
objOut.Close

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BravehearT-1326Author Commented:
Thanks for taking the time / effort to assist with this script issue.  It is much appreciated.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Server 2003

From novice to tech pro — start learning today.