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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Assistance with vbscript to query services for service account

Status
Not open for further replies.

mefanning

MIS
Apr 26, 2010
19
US
I have created a script to query services to retrieve their service account information. To begin, I execute the script locally and it does retrieve the correct information. However, I would like to modify the script to poll a list of services for their service account info and write the results to a csv file.
I have 3 issues I would like to address:

1. One issue I am having is how to properly setup the Do Loop. As it stands, the message I get is "loop without a do"
2. How to allow the script to proceed through an error with a host in the list. I believe I have the proper syntax, but I cannot verify without solving the loop issue.
3. How to modify the service polled to look for a wild card...For example, poll every service with the word "microsoft" in it.

I really appreciate the assistance.

Script below.

---------------------------

On Error Resume Next

Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.OpenTextFile("c:\service_list.csv", _
ForAppending, True)
Set objTextFile = objFSO.OpenTextFile("C:\list.txt",1)

objLogFile.Write _
("System Name,Service Name, Account Name ")
objLogFile.Writeline

strComputer = objTextFile.Readline

Do Until objTextFile.AtEndOfStream

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service where Name='Alerter'")
Set objLogFile = objFSO.OpenTextFile("c:\service_list.csv", _
ForAppending, True)

For Each objService in colListOfServices
objLogFile.Write(objService.SystemName) & ","
objLogFile.Write(objService.Name) & ","
objLogFile.Write(objService.StartName) & ","
Loop
objLogFile.Close
 
Correction, I would like the script to poll a list of servers, not services...

Thank you.
 
1. your "for" loop has no "next". this is the reason you are getting a "loop without a do" error

2. On Error Resume Next. Fix 1 and 2 will work.

3. Use an if/then inside the for loop.

Code:
   if (inStr(objService.Name, "Microsoft")) then
      do stuff
   else
      do other stuff
   end if

4. You'll also want to turn on the create flag and specify the text format of the log file to ASCII so you can actually read it.

object.OpenTextFile(filename[, iomode[, create[, format]]])
thus
Set objTextFile = objFSO.OpenTextFile("C:\list.txt", 1, true, 0)


-Geates
 
Geates, Thank you.

I tried to fix number 1, but it appears to continually loop in

objLogFile.Write(objService.SystemName) & ","
objLogFile.Write(objService.Name) & ","
objLogFile.Write(objService.StartName) & "

What I get is writing continually - SERVER,Alerter,NT AUTHORITY\LocalService,SERVER,Alerter,NT AUTHORITY\LocalService,SERVER,Alerter,NT AUTHORITY\LocalService,

I'm trying to get it to loop back to the top top of my input list of servers and repeat logging. In this instance, I have to kill wscript to move get it to quit.

New code looks like:

On Error Resume Next

Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.OpenTextFile("c:\service_list.csv", _
ForAppending, True)
Set objTextFile = objFSO.OpenTextFile("C:\list.txt",1)

objLogFile.Write _
("System Name,Service Name, Account Name ")
objLogFile.Writeline

strComputer = objTextFile.Readline

Do Until objTextFile.AtEndOfStream

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service where Name='Alerter'")
Set objLogFile = objFSO.OpenTextFile("c:\service_list.csv", _
ForAppending, True)

For Each objService in colListOfServices
objLogFile.Write(objService.SystemName) & ","
objLogFile.Write(objService.Name) & ","
objLogFile.Write(objService.StartName) & ","
Next
objLogFile.Close
Loop

-----------

I should warn you I am a noob at this.

Thank you.
 
Replace this:
strComputer = objTextFile.Readline
Do Until objTextFile.AtEndOfStream
with this:
Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.Readline

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Ok, so I am now attempting to modify the script to pick out key words in the service name as recommended by geates. I've added the If/End if, but it fails to retrieve any information. I don't get any errors, however.

-------On Error Resume Next

Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.OpenTextFile("c:\service_list.csv", _
ForAppending, True)
Set objTextFile = objFSO.OpenTextFile("C:\list.txt",1)

'objLogFile.Write _
' ("System Name,Service Name, Account Name ")
'objLogFile.Writeline

Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.Readline

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colListOfServices = objWMIService.ExecQuery _
' ("Select * from Win32_Service where Name='*'")

If (inStr(objService.Name, "Microsoft")) then
Set objLogFile = objFSO.OpenTextFile("c:\service_list.csv", _
ForAppending, True)

For Each objService in colListOfServices
objLogFile.Write(objService.SystemName) & ","
objLogFile.Write(objService.Name) & ","
objLogFile.Write(objService.StartName)
objLogFile.Writeline

Next
End If
objLogFile.Close
Loop
 
Got it...

On Error Resume Next

Const ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.OpenTextFile("c:\service_list.csv", _
ForAppending, True)
Set objTextFile = objFSO.OpenTextFile("C:\list.txt",1)

'objLogFile.Write _
' ("System Name,Service Name, Account Name ")
'objLogFile.Writeline

Do Until objTextFile.AtEndOfStream
strComputer = objTextFile.Readline

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colListOfServices = objWMIService.ExecQuery _
("Select * from Win32_Service where Name Like '%Alerter_%'")
Set objLogFile = objFSO.OpenTextFile("c:\service_list.csv", _
ForAppending, True)

For Each objService in colListOfServices
objLogFile.Write(objService.SystemName) & ","
objLogFile.Write(objService.Name) & ","
objLogFile.Write(objService.StartName)
objLogFile.Writeline
Next
objLogFile.Close
Loop
 
Code:
Set colListOfServices = objWMIService.ExecQuery _        ("Select * from Win32_Service where Name Like '%Alerter_%'")
Good line. Way to use "Like"

when looking for a text within another text, it's a good idea to either ucase or lcase the text to increase match confidence.

inStr(ucase(objService.Name, "MICROSOFT"))
inStr(lcase(objService.Name, "microsoft"))

-Geates
 
PHV, I assume that satifies case sensitivity when using inStr by returning a neg, zero, or pos int?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top