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

script for querying which services run under administrator account?


Anyone have a script that can be run on the server console that will report back which of the local services use the domain administrator account for 'log on as' ?   ideally, the value one searches on would be a prompted input in the script ("please enter domain\username to match")  

or even better, it would simply output the username used for all local services to a csv then I could check after the fact. (service name, starts-in-path, "log on as" user, startup-type.

would be useful in planning for high-level account password changes.

bonus: run on one server, and prompt the user for hostnames to query, or pull those hostnames from a text file (host1 <cr> host2 <cr>) assuming the user you ran the script on host0 had the rights to query the other hosts.

1 Solution
If Powershell is installed then this will work:

get-WmiObject win32_service | format-table Name, StartName -auto | out-file "C:\services.txt"  

Running it remotely can be done if Powershell is installed on the remote server using PSEXEC.

I'm sure there is a way to do this remotely using only powershell, but I'm a bit busy right now.

The script below will dump all service accounts to a text file.  Then you parse the text file for the string you want.  Save the text below as script.vbs.  Create a file called 'servers.txt' and populate it with a list of servers - start with a test server first.    Then, from your computer go to a cmd prompt and type:

cscript script.vbs

The output file is svcaccounts.csv and it gets created.

Then open svcaccounts.csv in Excel and sort by the field that lists the administrator account and delete the rest.  

On Error Resume Next
Dim oSvc,oPC, objNetwork
dim objFSO,objFile,objFile2

Const ForReading=1
Const ForWriting=2
set objNetwork=CreateObject("Wscript.Network")

ServerList="servers.txt"            'name of file with server names to check.
OutputFile="svcaccounts.csv"      'file to hold results

set objFSO=CreateObject("Scripting.FileSystemObject")
set objFile=objFSO.OpenTextfile(serverlist,forReading)
set objFile2=objFSO.CreateTextFile(OutPutfile)

do while objFile.AtEndofStream<>True
 wscript.echo "Enumerating Service Accounts on " & sTarget

Set oPC=GetObject("WinNT://"&sTarget)
'verify connectivity
If err.number<>0 Then
   wscript.echo "Error connecting to " & sTarget
   wscript.echo "Error #"&Err.Number & ": " & Err.Description
End If

objFile2.WriteLine "Server,Display Name,Service Name,Service Account Name"
 For Each svc In oPC
  objFile2.WriteLine sTarget &"," & svc.DisplayName & "," & svc.Name & "," & svc.ServiceAccountName

set oPC=Nothing
set objNetwork=Nothing



wscript.echo "See " & OutPutfile & " for results."


hakalugiAuthor Commented:

works great, exactly what I was looking for.

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

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.

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