Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check services on array of computers 1

Status
Not open for further replies.

hdxpdd

IS-IT--Management
Jun 17, 2012
4
Hello

I'm relatively new to scripting and trying to find out if a service exists on a number of Servers but I'm stuck and can't figure out why the following code is not working
Code:
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile("c:\test\Servernames.txt", 1)

Do until oFile.AtEndOfStream
strNextLine = oFile.Readline
arrServer = split(strNextLine, ",")
Loop
oFile.Close

For Each strServer in arrServer
	Set objWMIService = GetObject("winmgmts:" & "{ImpresonationLevel=impresonate}!\\" & strServer & "\root\cimv2")
	Set sCol = objWMIServices.ExecQuery("SELECT * FROM Win32_Service where Name = 'W3SVC'")
		if sCol.count = 0 then
			Wscript.Echo = strServer & ", " & Now & ", " & "Service Does not Exists"
		Else
			Wscript.Echo = strServer & ", " & Now & ", " & "Service Exists"
		End if	
next

I would appreciate if to hear your comments.
Thanks
 
The server names separated by comma. Simple names not FQDN. The first part works by itself but there appears that there is something wrong with the second at least that is where I get an error - expected ")" where I don't see how it should be there.
 
>The server names separated by comma

So, it looks like this (all one line, no carriage returns etc)???
Code:
SERVER1,SERVER2,SERVER3

If so, change the first part to
Code:
Const ForReading = 1
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile("c:\test\Servernames.txt", ForReading)
arrServer = Split(oFile.ReadAll, ",")
 
Hi, thanks for your reply. I modified the code as you suggested and corrected my spelling mistakes ^_^ it now looks like this

Code:
Option Explicit

Dim oFSO, oFile, arrServer, strServer, objWMIService, sCol

Const ForReading = 1
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile("c:\test\Servernames.txt", ForReading)
Do until oFile.AtEndOfStream
arrServer = split(oFile.ReadAll, ",")

Loop
oFile.Close

For Each strServer in arrServer
	Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strServer & "\root\cimv2")
	Set sCol = objWMIService.ExecQuery("SELECT * FROM Win32_Service where Name = 'W3SVC'")
		if sCol.count = 0 then
			Wscript.Echo = strServer.name & ", " & Now & ", " & "Service Does not Exists"
		Else
			Wscript.Echo = strServer.name & ", " & Now & ", " & "Service Exists"
		End if	
next

And now as I try to get the final output I get:
Line: 18
Char: 4
Error: Object Required:'SERVER1'

If I leave it without the just "strServer"
I get "Object does not support property or Method: Wscript.Echo"

There must be something I'm missing but what? Any thoughts?
 
First, there is no need to loop until "AtEndOfStream", since you are reading the entire file in one shot (oFile.ReadAll). See my suggestion earlier. (Although, your code may still work, since I guess you ARE at AtEndOfStream... just wanted to clarify that).

Your error is that there should be no = after the echo:

Code:
if sCol.count = 0 then
   Wscript.Echo strServer.name & ", " & Now & ", " & "Service Does not Exists"
Else
   Wscript.Echo strServer.name & ", " & Now & ", " & "Service Exists"
End if
 
Hi,

I ran the script but got an error:
------------
C:\Test\scripts>cscript check-svcs.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\Test\scripts\check-svcs.vbs(20, 4) Microsoft VBScript runtime error: Objec
t required: 'SVR0275'
------------

where 'SVR0275' is the server name

Servernames.txt is a csv file, right?

Please help. Thanks in advance!



 
Replace this:
strServer.name
with this:
strServer

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Servernames.txt is a csv file, right?
Well, yes, in that it is a series of values separated by commas. That's just how the OP structured their input file. But it doesn't have to be that way... Servernames.txt is just a text file that is being parsed to extract the names of the servers. More typically, the input file would contain one server per line, like:

Code:
Server1
Server2
Server3

If that's how Servernames.txt was structured, the code to read it and loop through each server would be:
Code:
Option Explicit
Const ForReading = 1
Dim oFSO, oFile, arrServer, strServer
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.OpenTextFile("c:\test\Servernames.txt", ForReading)
arrServer = Split(oFile.ReadAll, [COLOR=blue]vbCrLf[/color])
For Each strServer in arrServer
   [COLOR=green]' do something with strServer [/color]
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top