Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Use Variable to retrieve user information

Posted on 2009-02-13
5
Medium Priority
?
237 Views
Last Modified: 2012-05-06
The end purpose will be to supply a list of usernames {SS or ASCI} and pipe them to this script which will either update the users home directories {& TS Home Directory} to the new location.
For testing purposes I am only displaying the data.

The code below works when I specify the SAMAccountName
[ "AND SamAccountName = 'mjones'" ]

However, I need to be able to put a variable [instead of mjones] and I am getting lost with how to construct the quotes and such.    I want it to read.
               "AND SamAccountName = 'strUserName' "

Your help is appreciated.

On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2
strUserName = "mjones"
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 
objCommand.CommandText = _
    "SELECT distinguishedName FROM 'LDAP://dc=brookfieldproperties,dc=com' WHERE objectCategory='user' " &_
          "AND SamAccountName = 'mjones'"
     Set objRecordSet = objCommand.Execute	
     objRecordSet.MoveFirst
     Do Until objRecordSet.EOF
	strDN = objRecordSet.Fields("distinguishedName").Value
	objRecordSet.MoveNext
     Loop
	Set objUser = GetObject("LDAP://" & strDN)
	Wscript.Echo "    Home Directory: " & objUser.HomeDirectory
	Wscript.Echo "        Users Name: " & objUser.Name
	Wscript.Echo "   User Home Drive: " & objUser.HomeDrive
	Wscript.Echo " User Display Name: " & objUser.Displayname
	Wscript.Echo "  SAM Account Name: " & objUser.samaccountname
	Wscript.Echo " TS Home Directory: " & objUser.TerminalServicesHomeDirectory
	Wscript.Echo "Distinguished Name: " & objUser.distinguishedname

Open in new window

0
Comment
Question by:MAJAEJ
  • 3
5 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 23634834
strSAM = "mjones"
objCommand.CommandText = _
    "SELECT distinguishedName FROM 'LDAP://dc=brookfieldproperties,dc=com' WHERE objectCategory='user' " &_
          "AND SamAccountName = '" & strSAM & "'"
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 2000 total points
ID: 23634849

'(Remove this line unless you expect errors...it can cause trouble...On Error Resume Next
Const ADS_SCOPE_SUBTREE = 2
strUserName = "mjones"
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 
objCommand.CommandText = _
    "SELECT distinguishedName FROM 'LDAP://dc=brookfieldproperties,dc=com' WHERE objectCategory='user' " &_
          "AND SamAccountName = '" & strUserName & "'"
     Set objRecordSet = objCommand.Execute      
     objRecordSet.MoveFirst
     Do Until objRecordSet.EOF
        strDN = objRecordSet.Fields("distinguishedName").Value
        objRecordSet.MoveNext
     Loop
        Set objUser = GetObject("LDAP://" & strDN)
        Wscript.Echo "    Home Directory: " & objUser.HomeDirectory
        Wscript.Echo "        Users Name: " & objUser.Name
        Wscript.Echo "   User Home Drive: " & objUser.HomeDrive
        Wscript.Echo " User Display Name: " & objUser.Displayname
        Wscript.Echo "  SAM Account Name: " & objUser.samaccountname
        Wscript.Echo " TS Home Directory: " & objUser.TerminalServicesHomeDirectory
        Wscript.Echo "Distinguished Name: " & objUser.distinguishedname

Open in new window

0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 23634869

Like this:

    "SELECT distinguishedName FROM 'LDAP://dc=brookfieldproperties,dc=com' WHERE objectCategory='user' " &_
          "AND SamAccountName='" & strUsername & "'"

Be a little careful with that above, if you look at the quotes carefully we still have the single quotes (') surrounding the username.

It won't be as simple as Piping (e.g. userlist.txt | <TheScript>) because VbScript just won't understand it. However, you can teach it to read from a file quite easily.

Otherwise, you might look at PowerShell, which will allow all kinds of piping.

HTH

Chris
0
 

Author Closing Comment

by:MAJAEJ
ID: 31546688
Thanks - the quotes around that argument were driving me nuts.  Makes total sense looking at it and the answer was basically there above in the SELECT statement but I just couldn't see it...

Thanks
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 23658729
Happy to help - thanx for the grade! ;^)
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

Here's a look at newsworthy articles and community happenings during the last month.
How to deal with a specific error when using the Enable-RemoteMailbox cmdlet to create a mailbox in the cloud-based service, for an existing user in an on-premises Active Directory.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

810 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