?
Solved

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

Posted on 2008-11-19
7
Medium Priority
?
1,221 Views
Last Modified: 2012-05-05
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
Comment
Question by:BravehearT-1326
  • 3
  • 2
6 Comments
 

Author Comment

by:BravehearT-1326
ID: 22995302
I understand I could just run the following for each of the servers concerned as well....

sc \\servernamehere query "ServiceNameGoesHere" | find /i "running"
0
 

Author Comment

by:BravehearT-1326
ID: 22995812
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
 
LVL 71

Expert Comment

by:Qlemo
ID: 23151691
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
LVL 67

Expert Comment

by:sirbounty
ID: 23151853
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
 
LVL 67

Accepted Solution

by:
sirbounty earned 750 total points
ID: 23151878
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
 

Author Closing Comment

by:BravehearT-1326
ID: 31518282
Thanks for taking the time / effort to assist with this script issue.  It is much appreciated.
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's recap what we learned from yesterday's Skyport Systems webinar.
It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Suggested Courses
Course of the Month16 days, 8 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question