Solved

VB Script to Check Administrator Group on a List of PC's

Posted on 2011-03-11
10
413 Views
Last Modified: 2012-05-11
Hello - I need a VB script that will run against a list of PC's and report back which users/groups are in the Administrators Group on the PC's.
0
Comment
Question by:vhaperbaugub
  • 5
  • 5
10 Comments
 
LVL 8

Expert Comment

by:jawa29
ID: 35110945
Hi

The code below should do the trick, just create a text file on you C: called ListOfPCs.txt with each PC on it's own line and this script will work.

Remember to run this as a Domain Admin or else you won't be able to query the PC remotely.

Jawa29


Const ForReading = 1
Const ForWriting = 2

On Error Resume Next

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFile = objFSO.OpenTextFile("C:\ListOfPCs.txt", ForReading)

Do Until oFile.AtEndOfStream
	sComputer = oFile.ReadLine
	sTxt = sTxt & "--" & sComputer & "--" & vbCRLF
	Set oGroup = GetObject("WinNT://" & sComputer & "/Administrators,group")
	For Each oUser in oGroup.Members
		sTxt = sTxt & oUser.Name & vbCRLF
	Next
	sTxt = sTxt & "----------"
Loop

Set oTextFile = oFSO.OpenTextFile("C:\ListOfPCs-Results.txt", ForWriting, True)
oTextFile.WriteLine(sTxt)
oTextFile.Close

Set oFSO = Nothing
Set oFile = Nothing
Set oGroup = Nothing

WScript.Quit

Open in new window

0
 

Author Comment

by:vhaperbaugub
ID: 35111090
Hello - I got this error: C:\FindAdmin.vbs(30, 25) Microsoft VBScript compilation error: Expected end of statement
0
 
LVL 8

Expert Comment

by:jawa29
ID: 35111121
Sorry Friday fingers in operation

Replace this line
Set oFile = objFSO.OpenTextFile("C:\ListOfPCs.txt", ForReading)


With this
Set oFile = oFSO.OpenTextFile("C:\ListOfPCs.txt", ForReading)

Code tested.

Jawa29

0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:vhaperbaugub
ID: 35111267
Hello - Now I'm getting this error:

C:\FindAdmin.vbs(31, 25) Microsoft VBScript compilation error: Expected end of statement
0
 
LVL 8

Expert Comment

by:jawa29
ID: 35111304
Can you paste your exact code please?

Jawa29
0
 

Author Comment

by:vhaperbaugub
ID: 35111444
Const ForReading = 1
Const ForWriting = 2

On Error Resume Next

Set oFSO = CreateObject("Scripting.FileSystemObject")

Set oFile = oFSO.OpenTextFile("C:\ListOfPCs.txt", ForReading)


Do Until oFile.AtEndOfStream
      sComputer = oFile.ReadLine
      sTxt = sTxt & "--" & sComputer & "--" & vbCRLF
      Set oGroup = GetObject("WinNT://" & sComputer & "/Administrators,group")
      For Each oUser in oGroup.Members
            sTxt = sTxt & oUser.Name & vbCRLF
      Next
      sTxt = sTxt & "----------"
Loop

Set oTextFile = oFSO.OpenTextFile("C:\ListOfPCs-Results.txt", ForWriting, True)
oTextFile.WriteLine(sTxt)
oTextFile.Close

Set oFSO = Nothing
Set oFile = Nothing
Set oGroup = Nothing

WScript.Quit

Toggle HighlightingOpen in New WindowSelect All
0
 
LVL 8

Accepted Solution

by:
jawa29 earned 500 total points
ID: 35111477
Whats this?

Toggle HighlightingOpen in New WindowSelect All

Once the script hits WScript.Quit it will finish, remove the above mentioned line.

Jawa29
0
 

Author Comment

by:vhaperbaugub
ID: 35111559
That was wierd.....its working now....Thanks!!!!!!!!!!!!!
0
 

Author Comment

by:vhaperbaugub
ID: 35111982
Just wondering.....is there a way to add a progress bar or something to the script? When you do a couple thousand PC's at once the screen remains blank.....Thanks again.
0
 
LVL 8

Expert Comment

by:jawa29
ID: 35112080
The only real way to do that effectivly is to run the script using cscript and add a Wscript.Echo in there.

Something like:

Do Until oFile.AtEndOfStream
      sComputer = oFile.ReadLine
      sTxt = sTxt & "--" & sComputer & "--" & vbCRLF
      Set oGroup = GetObject("WinNT://" & sComputer & "/Administrators,group")
      For Each oUser in oGroup.Members
            sTxt = sTxt & oUser.Name & vbCRLF
      Next
      sTxt = sTxt & "----------"
      Wscript.Echo sComputer
Loop


If you add this then just double click the file it will constantly pop up a window with the computer name has just completed and not continue until you select OK.

I use VBSEdit which allows you to run scripts as cscript which outputs to a window.

If you want to run the script in script from a command window just type in cscript C:\scriptname.vbs

Jawa29

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to make an ADE file by code? 11 94
Compile Error 7 61
How can I run a vbs script to all members servers in a domain? 9 23
Having trouble with a VB Script 17 17
Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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