Saturday, September 29

Query Services for "Log On As" info

I needed to find if there is any service running with a service account on the Citrix servers and this script helped me on Windows 2000 & 2003 servers (ofcourse not my work, posting here for records)

-------------------------------------------------------------------------------------------------------------------------
Option Explicit ' force varibales to be declaired
 
' declair constant variables
Const FOR_READING = 1 ' declair OpenTextFile variables
Const FOR_WRITE = 2 ' declair OpenTextFile variables
Const FOR_APPENDING = 8 ' declair OpenTextFile variables

' declair variables
Dim objFSO, objNewFile, arrComputers, computer, objComputers, strAllComputers ' get computers variables
Dim objWMIService, colServices, objService ' get services variables
 
'create objects
Set objFSO = CreateObject("Scripting.FileSystemObject") ' create FSO object
Set objNewFile = objFSO.CreateTextFile("services.csv",TRUE) ' create output file
Set objComputers = objFSO.OpenTextFile("Computers.txt",FOR_READING) ' read computer list
 
strAllComputers = objComputers.ReadAll ' read entire file
arrComputers = split(strAllComputers, vbcrlf) ' parse file content into an Array
 
'table headers
objNewFile.WriteLine "Computer Name,Service Name,RunAs" ' create csv table headers
On Error Resume Next
' loop all computers
For Each computer In arrComputers
'list services & log-on-as
Set objWMIService = GetObject _
("winmgmts:\\" & computer & "\root\cimv2")
Set colServices = objWMIService.ExecQuery _
("Select * From Win32_Service")
For Each objService in colServices
objNewFile.WriteLine computer & "," & objService.Name & "," & objService.StartName
Next
'list scheduled tasks & log-on-as
Next
     
' close object
objNewFIle.Close

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

No comments:

Post a Comment