Solved

VBscript to output to a file accounts enabled in AD

Posted on 2006-06-30
14
941 Views
Last Modified: 2012-08-14
I have a script that goes and retrieves usernames from AD that are enabled and displays them in an output box but i want it to output to a text file... can someone help me its urgent?

here is the script:
'This script should search for all user account in the domain
'that aren't disabled. The sAMAccountType=8053006368 is for
'all the different accounts. 1.2.840.113556.1.4.803 is the BIT_AND search
'so with this LDAP Query we are looking for all accounts that don't have
'the 2 bit turned on (Notice the ! (NOT) in front if the userAccountControl)
'You will need to rename the file so its a .vbs and enter your domain name
'in the Query string.

Option Explicit
Dim aCon, aCmd, aRst, sResultText
sResultText = "Enabled Accounts" & vbCrLf
Set aCon = CreateObject("ADODB.Connection")
Set aCmd = CreateObject("ADODB.Command")
aCon.Provider = "ADsDSOObject"  
aCon.Open       
aCmd.ActiveConnection = aCon       
aCmd.CommandText = "<LDAP://DC=company,DC=com>;" _
  & 

"(&(objectClass=person)(objectClass=user)(sAMAccountType=805306368)(!userAccountControl:

1.2.840.113556.1.4.803:=2));" _
      & "name,SamAccountName,givenName,sN;subTree"
Set aRst = aCmd.Execute()
Do While Not aRst.EOF
     sResultText = sResultText _
            & aRst.Fields("samaccountname") _
            & ", " & aRst.Fields("givenname") _
            & ", "       & aRst.Fields("sN") _
            & vbCRLF      
      aRst.MoveNext
Loop
Dim oFilesys, oFiletxt, sFilename, sPath

Set oFilesys = CreateObject("Scripting.FileSystemObject")

Set oFiletxt = oFilesys.CreateTextFile("c:\test.txt", True)

sPath = oFilesys.GetAbsolutePathName("c:\test.txt")

sFilename = oFilesys.GetFileName(sPath)

oFiletxt.WriteLine("Accounts Enabled.")

oFiletxt.Close'

If oFilesys.FileExists(sPath) Then Wscript.Echo sResultText
0
Comment
Question by:rajsidhu
  • 9
  • 5
14 Comments
 
LVL 8

Expert Comment

by:Shakti109
ID: 17019691

Find below the altered script to also add the same output to the textfile that you defined.
This will echo the exact same information to the textfile as that being displayed in your box.

------------------------------------------------------------------------------
Option Explicit
Dim aCon, aCmd, aRst, sResultText

Dim oFilesys, oFiletxt, sFilename, sPath
Set oFilesys = CreateObject("Scripting.FileSystemObject")
Set oFiletxt = oFilesys.CreateTextFile("c:\test.txt", True)
sPath = oFilesys.GetAbsolutePathName("c:\test.txt")
sFilename = oFilesys.GetFileName(sPath)

sResultText = "Enabled Accounts" & vbCrLf
Set aCon = CreateObject("ADODB.Connection")
Set aCmd = CreateObject("ADODB.Command")
aCon.Provider = "ADsDSOObject"  
aCon.Open      
aCmd.ActiveConnection = aCon      
aCmd.CommandText = "<LDAP://DC=company,DC=com>;" _
  & 

"(&(objectClass=person)(objectClass=user)(sAMAccountType=805306368)(!userAccountControl:

1.2.840.113556.1.4.803:=2));" _
     & "name,SamAccountName,givenName,sN;subTree"
Set aRst = aCmd.Execute()
Do While Not aRst.EOF
     sResultText = sResultText _
          & aRst.Fields("samaccountname") _
          & ", " & aRst.Fields("givenname") _
          & ", "      & aRst.Fields("sN") _
          & vbCRLF
         
          ofiletext.writeline(
          & aRst.Fields("samaccountname") _
          & ", " & aRst.Fields("givenname") _
          & ", "      & aRst.Fields("sN") _
          & vbCRLF )
         
     aRst.MoveNext
Loop

oFiletxt.WriteLine("Accounts Enabled.")
oFiletxt.Close'
If oFilesys.FileExists(sPath) Then Wscript.Echo sResultText

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

Any ? let me know.
0
 
LVL 8

Expert Comment

by:Shakti109
ID: 17019700
Whoops, there is a typo in the above....

The section that reads :

          ofiletext.writeline(
          & aRst.Fields("samaccountname") _
          & ", " & aRst.Fields("givenname") _
          & ", "      & aRst.Fields("sN") _
          & vbCRLF )

Should read :

          ofiletext.writeline( aRst.Fields("samaccountname") _
          & ", " & aRst.Fields("givenname") _
          & ", "      & aRst.Fields("sN") _
          & vbCRLF )

0
 

Author Comment

by:rajsidhu
ID: 17019874
i get a compilation error
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.

 
LVL 8

Expert Comment

by:Shakti109
ID: 17019955

You created the text file, copied in the text, renamed it as a .vbs and then ran it and it gave you an error?

Which line does it say it is erroring on?
0
 
LVL 8

Expert Comment

by:Shakti109
ID: 17019991

The compilation error you got was most likely on line 17, and has to do with the cut-n-past and the extra spaces / line breaks of these text boxes.

The following line of code (which shows as two here) should be on one single line in your text editor :

  & "(&(objectClass=person)(objectClass=user)(sAMAccountType=805306368)(!userAccountControl:1.2.840.113556.1.4.803:=2));" _



0
 
LVL 8

Expert Comment

by:Shakti109
ID: 17020057

If you cut-n-paste the following, verbatim, you should'nt have any problems (compiled and recompiled a few times) :

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

Option Explicit
Dim aCon, aCmd, aRst, sResultText

Dim oFilesys, oFiletxt, sFilename, sPath
Set oFilesys = CreateObject("Scripting.FileSystemObject")
Set oFiletxt = oFilesys.CreateTextFile("c:\test.txt", True)
sPath = oFilesys.GetAbsolutePathName("c:\test.txt")
sFilename = oFilesys.GetFileName(sPath)

sResultText = "Enabled Accounts" & vbCrLf
Set aCon = CreateObject("ADODB.Connection")
Set aCmd = CreateObject("ADODB.Command")
aCon.Provider = "ADsDSOObject"  
aCon.Open      
aCmd.ActiveConnection = aCon      
aCmd.CommandText = "<LDAP://DC=company,DC=com>;" _
  & "(&(objectClass=person)(objectClass=user)(sAMAccountType=805306368)(!userAccountControl:1.2.840.113556.1.4.803:=2));" _
 & "name,SamAccountName,givenName,sN;subTree"
Set aRst = aCmd.Execute()
Do While Not aRst.EOF
     sResultText = sResultText _
          & aRst.Fields("samaccountname") _
          & ", " & aRst.Fields("givenname") _
          & ", "      & aRst.Fields("sN") _
          & vbCRLF
         
          ofiletext.writeline(aRst.Fields("samaccountname") _
          & ", " & aRst.Fields("givenname") _
          & ", "      & aRst.Fields("sN") _
          & vbCRLF )
         
     aRst.MoveNext
Loop


oFiletxt.WriteLine("Accounts Enabled.")
oFiletxt.Close'
If oFilesys.FileExists(sPath) Then Wscript.Echo sResultText

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

This should remove the extra spaces/line breaks that are cauing the compile error after your paste.
0
 

Author Comment

by:rajsidhu
ID: 17020334
i still get a compilation error at line 27 character 11
0
 
LVL 8

Expert Comment

by:Shakti109
ID: 17020503

The only way that would happen, is the line-break/space issue....

Replace the following section :

Do While Not aRst.EOF
     sResultText = sResultText _
          & aRst.Fields("samaccountname") _
          & ", " & aRst.Fields("givenname") _
          & ", "      & aRst.Fields("sN") _
          & vbCRLF
         
          ofiletext.writeline(aRst.Fields("samaccountname") _
          & ", " & aRst.Fields("givenname") _
          & ", "      & aRst.Fields("sN") _
          & vbCRLF )
         
     aRst.MoveNext
Loop


WITH :

Do While Not aRst.EOF
   sResultText = sResultText & aRst.Fields("samaccountname") & ", " & aRst.Fields("givenname") & ", " & aRst.Fields("sN") & vbCRLF
   ofiletext.writeline(aRst.Fields("samaccountname") & ", " & aRst.Fields("givenname") & ", " & aRst.Fields("sN") & vbCRLF )
   aRst.MoveNext
Loop


This has no line-continuations, breaks, spaces, or anything else. The issues you are experiencing are line-break/space/cut-n-paste related.

If the above does not work for you, we can find a way to directly transfer the file itself.
0
 

Author Comment

by:rajsidhu
ID: 17020547
i get an error still that says:
Line 22
Char 4
Error: variable is undefined: 'ofiletext'
0
 
LVL 8

Expert Comment

by:Shakti109
ID: 17020575

Hehe, this time it was MY cut-n-paste that had the error...

To make it easy (keeping the other line continuations/etc out), replace :

Do While Not aRst.EOF
   sResultText = sResultText & aRst.Fields("samaccountname") & ", " & aRst.Fields("givenname") & ", " & aRst.Fields("sN") & vbCRLF
   ofiletext.writeline(aRst.Fields("samaccountname") & ", " & aRst.Fields("givenname") & ", " & aRst.Fields("sN") & vbCRLF )
   aRst.MoveNext
Loop

WITH :

Do While Not aRst.EOF
   sResultText = sResultText & aRst.Fields("samaccountname") & ", " & aRst.Fields("givenname") & ", " & aRst.Fields("sN") & vbCRLF
   oFiletxt.writeline(aRst.Fields("samaccountname") & ", " & aRst.Fields("givenname") & ", " & aRst.Fields("sN") & vbCRLF )
   aRst.MoveNext
Loop

0
 

Author Comment

by:rajsidhu
ID: 17020598
that worked but can you sort them alphabetically?
0
 
LVL 8

Expert Comment

by:Shakti109
ID: 17020622

It's possible, but would require an array that could get very large depending on the number of members you got back.

What field are you wanting to sort on and can you give an example of how it is returned.

Examples:

Sort on "givenname",  comes back as "Doe, John"
Sort on "samaccountname", comes back as "GuestAccount"

etc...

You could also copy the contents of the text file into something like excel (using the comma as a delimiter) and sort the rows that way.

0
 

Author Comment

by:rajsidhu
ID: 17020659

current output:
johnll, John, llllliel

stepha, Steph, axxxx

jamed, Jame, Ddddxxt

if:
username: johna first name: john lastname: abercrob

desired output should be:
abercrob, John, johna or something like that but sort by last name
0
 
LVL 8

Accepted Solution

by:
Shakti109 earned 500 total points
ID: 17020716

Just to make sure, you are getting back
(as an example) :

samaccountname : Johna
Givenname : John Abercrob

And it is the "givenname" field you want to do the sorting on?

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

831 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