Link to home
Start Free TrialLog in
Avatar of rajsidhu
rajsidhu

asked on

VBscript to output to a file accounts enabled in AD

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
Avatar of Shakti109
Shakti109


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.
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 )

Avatar of rajsidhu

ASKER

i get a compilation error

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?

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));" _




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.
i still get a compilation error at line 27 character 11

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.
i get an error still that says:
Line 22
Char 4
Error: variable is undefined: 'ofiletext'

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

that worked but can you sort them alphabetically?

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.


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
ASKER CERTIFIED SOLUTION
Avatar of Shakti109
Shakti109

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial