Solved

How do I change a VBScript to read input and create an output file?

Posted on 2008-10-13
8
251 Views
Last Modified: 2012-06-21
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

Open in new window

0
Comment
Question by:mcpp661
  • 4
  • 3
8 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 22704029
Option Explicit
'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.FileSystemObject")
Set ts = fso.CreateTextFile("c\folder\subfolder\results.txt", True)

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)
    ts.WriteLine "User :" & strUser & Right(arrPath(1), intNameLength)
    objRecordSet.MoveNext
Loop

ts.Close
Set ts = Nothing
Set fso = Nothing
0
 
LVL 18

Expert Comment

by:exx1976
ID: 22704038
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
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
InName = "c:\source.txt"
OutName = "c:\destination.txt"
Set oFS = CreateObject("Scripting.FileSystemObject")
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=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
          data =  "User :" & strUser & Right(arrPath(1), intNameLength)
          OutFile.WriteLine(data)
          objRecordSet.MoveNext
      Loop
Loop
InFile.Close
OutFile.Close
0
 

Author Comment

by:mcpp661
ID: 22704245
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.
0
 
LVL 18

Expert Comment

by:exx1976
ID: 22704316
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.


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:mcpp661
ID: 22704472
I never used that script before, I just found that code on the Internet. Yeah, the DN is what I need. Thanks man.
0
 
LVL 18

Expert Comment

by:exx1976
ID: 22704547
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.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
InName = "c:\source.txt"
OutName = "c:\destination.txt"
DomainName = "mydomain"
Set oFS = CreateObject("Scripting.FileSystemObject")
Set InFile = oFS.OpenTextFile(InName,1)
Set OutFile = oFS.OpenTextFile(OutName,2)
Do While Not Infile.AtEndOfStream
      strUser = InFile.ReadLine
      data = GetObjectDN(strUser,DomainName)
    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("NameTranslate")
       objNameTranslate.Init ADS_NAME_INITTYPE_GC, ""
      objNameTranslate.Set ADS_NAME_TYPE_NT4, strdomain & "\" & strObject
      strObjectDN = objNameTranslate.Get(ADS_NAME_TYPE_1779)
      If Err.Number <> 0 Then
            strObjectDN = ""
      End If
       Set objNameTranslate = Nothing
      On Error Goto 0
       GetObjectDN = strObjectDN
End Function
0
 
LVL 18

Accepted Solution

by:
exx1976 earned 500 total points
ID: 22704561
actually, use this.  Sorry, I forgot to rip out all that ADODB stuff from the previous script.  My apologies.



InName = "c:\source.txt"
OutName = "c:\destination.txt"
DomainName = "mydomain"
Set oFS = CreateObject("Scripting.FileSystemObject")
Set InFile = oFS.OpenTextFile(InName,1)
Set OutFile = oFS.OpenTextFile(OutName,2)
Do While Not Infile.AtEndOfStream
      strUser = InFile.ReadLine
      data = GetObjectDN(strUser,DomainName)
    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("NameTranslate")
       objNameTranslate.Init ADS_NAME_INITTYPE_GC, ""
      objNameTranslate.Set ADS_NAME_TYPE_NT4, strdomain & "\" & strObject
      strObjectDN = objNameTranslate.Get(ADS_NAME_TYPE_1779)
      If Err.Number <> 0 Then
            strObjectDN = ""
      End If
       Set objNameTranslate = Nothing
      On Error Goto 0
       GetObjectDN = strObjectDN
End Function
0
 

Author Comment

by:mcpp661
ID: 22705069
Worked like a charm. Thanks man.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

867 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

22 Experts available now in Live!

Get 1:1 Help Now