mcpp661
asked on
How do I change a VBScript to read input and create an output file?
I have the following code which will determine the OU that a user is in. However, the way it's written it prompts for the username and outputs the OU via WScript.echo. How can I change this to so that it reads the input from a file and send the output to a text file? Thanks.
Option Explicit
'On Error Resume Next
Dim objConnection, objCommand, objRecordSet
Dim FindUser, strUser, strDN, arrPath, intLength, intNameLength
Const ADS_SCOPE_SUBTREE = 2
Const intForReading = 1
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
FindUser = InputBox("Please Enter A UserName", "Find User OU")
If FindUser = "" Then
MsgBox("No UserName Was Added")
WScript.Quit
Else
strUser = FindUser
End If
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.CommandText = _
"SELECT distinguishedName FROM 'LDAP://dc=domain.com,dc=com'WHERE objectCategory='user'AND sAMAccountName='" & strUser & "'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strDN = objRecordSet.Fields("distinguishedName").Value
arrPath = Split(strDN, ",")
intLength = Len(arrPath(1))
intNameLength = intLength - 3
Wscript.Echo "User :" & strUser & Right(arrPath(1), intNameLength)
objRecordSet.MoveNext
Loop
Const ADS_SCOPE_SUBTREE = 2
Const intForReading = 1
Set objConnection = CreateObject("ADODB.Connec tion")
Set objCommand = CreateObject("ADODB.Comman d")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnectio n = objConnection
objCommand.Properties("Pag e Size") = 1000
objCommand.Properties("Sea rchscope") = ADS_SCOPE_SUBTREE
InName = "c:\source.txt"
OutName = "c:\destination.txt"
Set oFS = CreateObject("Scripting.Fi leSystemOb ject")
Set InFile = oFS.OpenTextFile(InName,1)
Set OutFile = oFS.OpenTextFile(OutName,2 )
Do While Not Infile.AtEndOfStream
strUser = InFile.ReadLine
objCommand.CommandText = _
"SELECT distinguishedName FROM 'LDAP://dc=domain.com,dc=c om'WHERE objectCategory='user'AND sAMAccountName='" & strUser & "'"
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strDN = objRecordSet.Fields("disti nguishedNa me").Value
arrPath = Split(strDN, ",")
intLength = Len(arrPath(1))
intNameLength = intLength - 3
data = "User :" & strUser & Right(arrPath(1), intNameLength)
OutFile.WriteLine(data)
objRecordSet.MoveNext
Loop
Loop
InFile.Close
OutFile.Close
Const intForReading = 1
Set objConnection = CreateObject("ADODB.Connec
Set objCommand = CreateObject("ADODB.Comman
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnectio
objCommand.Properties("Pag
objCommand.Properties("Sea
InName = "c:\source.txt"
OutName = "c:\destination.txt"
Set oFS = CreateObject("Scripting.Fi
Set InFile = oFS.OpenTextFile(InName,1)
Set OutFile = oFS.OpenTextFile(OutName,2
Do While Not Infile.AtEndOfStream
strUser = InFile.ReadLine
objCommand.CommandText = _
"SELECT distinguishedName FROM 'LDAP://dc=domain.com,dc=c
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strDN = objRecordSet.Fields("disti
arrPath = Split(strDN, ",")
intLength = Len(arrPath(1))
intNameLength = intLength - 3
data = "User :" & strUser & Right(arrPath(1), intNameLength)
OutFile.WriteLine(data)
objRecordSet.MoveNext
Loop
Loop
InFile.Close
OutFile.Close
ASKER
exx1976....I used your code and it works........however the output is garbled..........do you know how to fix it so that it just displays the entire path of the user object? Thanks.
The output should be Exactly what you were getting before, I just cut and pasted in what you had before.. It looks like what you're doing is tryign to feed it a text file of user account names, and you want a text file of DN's back. Is that correct? If so, I have some code that will accomplish that, let me know if that's it and I'll throw it on here.
Otherwise, the line that starts with "data = " is the line to modify that controls the contents of the output file.
Otherwise, the line that starts with "data = " is the line to modify that controls the contents of the output file.
ASKER
I never used that script before, I just found that code on the Internet. Yeah, the DN is what I need. Thanks man.
This will output ONLY the DN, one per line.
You'll need to modify the lines "InName=" "OutName=" and "DomainName=" in order for it to work. DomainName should be the NetBIOS name of the domain, not the FQDN.
Const ADS_SCOPE_SUBTREE = 2
Const intForReading = 1
Set objConnection = CreateObject("ADODB.Connec tion")
Set objCommand = CreateObject("ADODB.Comman d")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnectio n = objConnection
objCommand.Properties("Pag e Size") = 1000
objCommand.Properties("Sea rchscope") = ADS_SCOPE_SUBTREE
InName = "c:\source.txt"
OutName = "c:\destination.txt"
DomainName = "mydomain"
Set oFS = CreateObject("Scripting.Fi leSystemOb ject")
Set InFile = oFS.OpenTextFile(InName,1)
Set OutFile = oFS.OpenTextFile(OutName,2 )
Do While Not Infile.AtEndOfStream
strUser = InFile.ReadLine
data = GetObjectDN(strUser,Domain Name)
OutFile.WriteLine(data)
Loop
InFile.Close
OutFile.Close
Function GetObjectDN(strObject, strDomain)
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_1779 = 1
Const ADS_NAME_TYPE_NT4 = 3
On Error Resume Next : Err.Clear
Set objNameTranslate = CreateObject("NameTranslat e")
objNameTranslate.Init ADS_NAME_INITTYPE_GC, ""
objNameTranslate.Set ADS_NAME_TYPE_NT4, strdomain & "\" & strObject
strObjectDN = objNameTranslate.Get(ADS_N AME_TYPE_1 779)
If Err.Number <> 0 Then
strObjectDN = ""
End If
Set objNameTranslate = Nothing
On Error Goto 0
GetObjectDN = strObjectDN
End Function
You'll need to modify the lines "InName=" "OutName=" and "DomainName=" in order for it to work. DomainName should be the NetBIOS name of the domain, not the FQDN.
Const ADS_SCOPE_SUBTREE = 2
Const intForReading = 1
Set objConnection = CreateObject("ADODB.Connec
Set objCommand = CreateObject("ADODB.Comman
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnectio
objCommand.Properties("Pag
objCommand.Properties("Sea
InName = "c:\source.txt"
OutName = "c:\destination.txt"
DomainName = "mydomain"
Set oFS = CreateObject("Scripting.Fi
Set InFile = oFS.OpenTextFile(InName,1)
Set OutFile = oFS.OpenTextFile(OutName,2
Do While Not Infile.AtEndOfStream
strUser = InFile.ReadLine
data = GetObjectDN(strUser,Domain
OutFile.WriteLine(data)
Loop
InFile.Close
OutFile.Close
Function GetObjectDN(strObject, strDomain)
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_1779 = 1
Const ADS_NAME_TYPE_NT4 = 3
On Error Resume Next : Err.Clear
Set objNameTranslate = CreateObject("NameTranslat
objNameTranslate.Init ADS_NAME_INITTYPE_GC, ""
objNameTranslate.Set ADS_NAME_TYPE_NT4, strdomain & "\" & strObject
strObjectDN = objNameTranslate.Get(ADS_N
If Err.Number <> 0 Then
strObjectDN = ""
End If
Set objNameTranslate = Nothing
On Error Goto 0
GetObjectDN = strObjectDN
End Function
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Worked like a charm. Thanks man.
'On Error Resume Next
Dim objConnection, objCommand, objRecordSet
Dim FindUser, strUser, strDN, arrPath, intLength, intNameLength
Dim fso, ts
Const ADS_SCOPE_SUBTREE = 2
Const intForReading = 1
Set fso = CreateObject("Scripting.Fi
Set ts = fso.CreateTextFile("c\fold
Set objConnection = CreateObject("ADODB.Connec
Set objCommand = CreateObject("ADODB.Comman
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnectio
FindUser = InputBox("Please Enter A UserName", "Find User OU")
If FindUser = "" Then
MsgBox("No UserName Was Added")
WScript.Quit
Else
strUser = FindUser
End If
objCommand.Properties("Pag
objCommand.Properties("Sea
objCommand.CommandText = _
"SELECT distinguishedName FROM 'LDAP://dc=domain.com,dc=c
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strDN = objRecordSet.Fields("disti
arrPath = Split(strDN, ",")
intLength = Len(arrPath(1))
intNameLength = intLength - 3
' Wscript.Echo "User :" & strUser & Right(arrPath(1), intNameLength)
ts.WriteLine "User :" & strUser & Right(arrPath(1), intNameLength)
objRecordSet.MoveNext
Loop
ts.Close
Set ts = Nothing
Set fso = Nothing