• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1369
  • Last Modified:

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.
0
BravehearT-1326
Asked:
BravehearT-1326
  • 3
  • 2
1 Solution
 
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
 
QlemoBatchelor, 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
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

 
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
 
BravehearT-1326Author Commented:
Thanks for taking the time / effort to assist with this script issue.  It is much appreciated.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now