Solved

VBscript to output to a file accounts enabled in AD

Posted on 2006-06-30
14
937 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now