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.Connec tion")
Set aCmd = CreateObject("ADODB.Comman d")
aCon.Provider = "ADsDSOObject"
aCon.Open
aCmd.ActiveConnection = aCon
aCmd.CommandText = "<LDAP://DC=company,DC=com >;" _
&
"(&(objectClass=person)(ob jectClass= user)(sAMA ccountType =805306368 )(!userAcc ountContro l:
1.2.840.113556.1.4.803:=2) );" _
& "name,SamAccountName,given Name,sN;su bTree"
Set aRst = aCmd.Execute()
Do While Not aRst.EOF
sResultText = sResultText _
& aRst.Fields("samaccountnam e") _
& ", " & aRst.Fields("givenname") _
& ", " & aRst.Fields("sN") _
& vbCRLF
aRst.MoveNext
Loop
Dim oFilesys, oFiletxt, sFilename, sPath
Set oFilesys = CreateObject("Scripting.Fi leSystemOb ject")
Set oFiletxt = oFilesys.CreateTextFile("c :\test.txt ", True)
sPath = oFilesys.GetAbsolutePathNa me("c:\tes t.txt")
sFilename = oFilesys.GetFileName(sPath )
oFiletxt.WriteLine("Accoun ts Enabled.")
oFiletxt.Close'
If oFilesys.FileExists(sPath) Then Wscript.Echo sResultText
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.Connec
Set aCmd = CreateObject("ADODB.Comman
aCon.Provider = "ADsDSOObject"
aCon.Open
aCmd.ActiveConnection = aCon
aCmd.CommandText = "<LDAP://DC=company,DC=com
&
"(&(objectClass=person)(ob
1.2.840.113556.1.4.803:=2)
& "name,SamAccountName,given
Set aRst = aCmd.Execute()
Do While Not aRst.EOF
sResultText = sResultText _
& aRst.Fields("samaccountnam
& ", " & aRst.Fields("givenname") _
& ", " & aRst.Fields("sN") _
& vbCRLF
aRst.MoveNext
Loop
Dim oFilesys, oFiletxt, sFilename, sPath
Set oFilesys = CreateObject("Scripting.Fi
Set oFiletxt = oFilesys.CreateTextFile("c
sPath = oFilesys.GetAbsolutePathNa
sFilename = oFilesys.GetFileName(sPath
oFiletxt.WriteLine("Accoun
oFiletxt.Close'
If oFilesys.FileExists(sPath)
Whoops, there is a typo in the above....
The section that reads :
ofiletext.writeline(
& aRst.Fields("samaccountnam e") _
& ", " & aRst.Fields("givenname") _
& ", " & aRst.Fields("sN") _
& vbCRLF )
Should read :
ofiletext.writeline( aRst.Fields("samaccountnam e") _
& ", " & aRst.Fields("givenname") _
& ", " & aRst.Fields("sN") _
& vbCRLF )
The section that reads :
ofiletext.writeline(
& aRst.Fields("samaccountnam
& ", " & aRst.Fields("givenname") _
& ", " & aRst.Fields("sN") _
& vbCRLF )
Should read :
ofiletext.writeline( aRst.Fields("samaccountnam
& ", " & aRst.Fields("givenname") _
& ", " & aRst.Fields("sN") _
& vbCRLF )
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)(ob
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.Fi
Set oFiletxt = oFilesys.CreateTextFile("c
sPath = oFilesys.GetAbsolutePathNa
sFilename = oFilesys.GetFileName(sPath
sResultText = "Enabled Accounts" & vbCrLf
Set aCon = CreateObject("ADODB.Connec
Set aCmd = CreateObject("ADODB.Comman
aCon.Provider = "ADsDSOObject"
aCon.Open
aCmd.ActiveConnection = aCon
aCmd.CommandText = "<LDAP://DC=company,DC=com
& "(&(objectClass=person)(ob
& "name,SamAccountName,given
Set aRst = aCmd.Execute()
Do While Not aRst.EOF
sResultText = sResultText _
& aRst.Fields("samaccountnam
& ", " & aRst.Fields("givenname") _
& ", " & aRst.Fields("sN") _
& vbCRLF
ofiletext.writeline(aRst.F
& ", " & aRst.Fields("givenname") _
& ", " & aRst.Fields("sN") _
& vbCRLF )
aRst.MoveNext
Loop
oFiletxt.WriteLine("Accoun
oFiletxt.Close'
If oFilesys.FileExists(sPath)
--------------------------
This should remove the extra spaces/line breaks that are cauing the compile error after your paste.
ASKER
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("samaccountnam
& ", " & aRst.Fields("givenname") _
& ", " & aRst.Fields("sN") _
& vbCRLF
ofiletext.writeline(aRst.F
& ", " & aRst.Fields("givenname") _
& ", " & aRst.Fields("sN") _
& vbCRLF )
aRst.MoveNext
Loop
WITH :
Do While Not aRst.EOF
sResultText = sResultText & aRst.Fields("samaccountnam
ofiletext.writeline(aRst.F
aRst.MoveNext
Loop
This has no line-continuations, breaks, spaces, or anything else. The issues you are experiencing are line-break/space/cut-n-pas
If the above does not work for you, we can find a way to directly transfer the file itself.
ASKER
i get an error still that says:
Line 22
Char 4
Error: variable is undefined: 'ofiletext'
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("samaccountnam
ofiletext.writeline(aRst.F
aRst.MoveNext
Loop
WITH :
Do While Not aRst.EOF
sResultText = sResultText & aRst.Fields("samaccountnam
oFiletxt.writeline(aRst.Fi
aRst.MoveNext
Loop
ASKER
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.
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.Fi
Set oFiletxt = oFilesys.CreateTextFile("c
sPath = oFilesys.GetAbsolutePathNa
sFilename = oFilesys.GetFileName(sPath
sResultText = "Enabled Accounts" & vbCrLf
Set aCon = CreateObject("ADODB.Connec
Set aCmd = CreateObject("ADODB.Comman
aCon.Provider = "ADsDSOObject"
aCon.Open
aCmd.ActiveConnection = aCon
aCmd.CommandText = "<LDAP://DC=company,DC=com
&
"(&(objectClass=person)(ob
1.2.840.113556.1.4.803:=2)
& "name,SamAccountName,given
Set aRst = aCmd.Execute()
Do While Not aRst.EOF
sResultText = sResultText _
& aRst.Fields("samaccountnam
& ", " & aRst.Fields("givenname") _
& ", " & aRst.Fields("sN") _
& vbCRLF
ofiletext.writeline(
& aRst.Fields("samaccountnam
& ", " & aRst.Fields("givenname") _
& ", " & aRst.Fields("sN") _
& vbCRLF )
aRst.MoveNext
Loop
oFiletxt.WriteLine("Accoun
oFiletxt.Close'
If oFilesys.FileExists(sPath)
--------------------------
Any ? let me know.