Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


AD Query / Script Help / Easy

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,55125
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> AD Query / Script Help / Easy
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1] 2   next >   >>
Login
Message << Older Topic   Newer Topic >>
 AD Query / Script Help / Easy - 1/8/2008 9:27:14 AM   
  jrcochranusa


Posts: 27
Score: 0
Joined: 1/30/2007
Status: offline
The basic AD query below returns the "manager" attribute of a user account as a CN. Is there an easy way to return the display name instead from the Query?

Or...do I need to manipulate the results with script? (which I s#ck at) 


objCommand.CommandText = _
     "<GC://" & TargetDomain & ">;(&(objectCategory=person)(objectClass=user))" & _
     ";objectCategory,givenName,sn,cn,displayName,sAMAccountName,mailNickname,title," & _
     "department,physicalDeliveryOfficeName,company,streetAddress,l,st,c,postalCode," & _
     "telephoneNumber,manager,facsimileTelephoneNumber,extensionAttribute1,mail," & _
     "extensionAttribute2,distinguishedName,msExchHomeServerName;subtree"

_____________________________

Thanks,

jrc
 
 
Post #: 1
 
 RE: AD Query / Script Help / Easy - 1/8/2008 9:44:04 AM   
  ebgreen


Posts: 4971
Score: 31
Joined: 7/12/2005
Status: offline
looks to me like it is:

objCommand.CommandText = _
    "<GC://" & TargetDomain & ">;(&(objectCategory=person)(objectClass=user))" & _
    ";objectCategory,givenName,sn,cn,displayName,sAMAccountName,mailNickname,title," & _
    "department,physicalDeliveryOfficeName,company,streetAddress,l,st,c,postalCode," & _
    "telephoneNumber,manager,facsimileTelephoneNumber,extensionAttribute1,mail," & _
    "extensionAttribute2,distinguishedName,msExchHomeServerName;subtree"

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to jrcochranusa)
 
 
Post #: 2
 
 RE: AD Query / Script Help / Easy - 1/8/2008 9:47:20 AM   
  dm_4ever


Posts: 2641
Score: 46
Joined: 6/29/2006
From: Orange County, California
Status: offline
The manager attribute may simply be displayed/entered as the distinguished name....i.e. cn=xxxxx,xxxxxx,xxxxx which you could use it to bind to the managers user object and then display whatever it is you want.

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to jrcochranusa)
 
 
Post #: 3
 
 RE: AD Query / Script Help / Easy - 1/21/2008 9:49:09 AM   
  jrcochranusa


Posts: 27
Score: 0
Joined: 1/30/2007
Status: offline
ebgreen... nope, that would return the "displayname" for the user being queried. I need the "displayname" for the "manager" attribute for the user being queried.

dm 4ever...yes, i think that would work, but the problem is that i am too dumb to know how to do it. i literally cut and paste sections of code and change out as little as possible. i am a retired touring musician who ended up in the industry as some kind of cruel punishment (he he...j/k kind of)

If I could figure out how to insert "strDN" into the code below, I think I could make it work in a subroutine or soemthing. strDN is the manager attribute returned from my original query but the syntax isn't working for me....

strDN = "cn=Cochran\, Jim,ou=usr,ou=it,dc=domain,dc=com"
Set objUser = Getobject("LDAP:// " & strDN & ")
strManagerDisplayName = objUser.displayname

_____________________________

Thanks,

jrc

(in reply to dm_4ever)
 
 
Post #: 4
 
 RE: AD Query / Script Help / Easy - 1/22/2008 1:34:09 AM   
  ebgreen


Posts: 4971
Score: 31
Joined: 7/12/2005
Status: offline
What error are you getting?

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to jrcochranusa)
 
 
Post #: 5
 
 RE: AD Query / Script Help / Easy - 1/22/2008 2:49:05 AM   
  twilliamsen

 

Posts: 195
Score: 0
Joined: 1/18/2007
Status: offline
I guess I don't know what you are trying to accomplish as a result.


(in reply to jrcochranusa)
 
 
Post #: 6
 
 RE: AD Query / Script Help / Easy - 1/22/2008 5:16:26 AM   
  jrcochranusa


Posts: 27
Score: 0
Joined: 1/30/2007
Status: offline
EBgreen...

getproperty.vbs(5, 47) Microsoft VBScript compilation error: Unterminated string constant

_____________________________

Thanks,

jrc

(in reply to jrcochranusa)
 
 
Post #: 7
 
 RE: AD Query / Script Help / Easy - 1/22/2008 5:27:37 AM   
  ebgreen


Posts: 4971
Score: 31
Joined: 7/12/2005
Status: offline
Good. That means your problem is a simple syntax error regarding quotes. Please post the script in it's entirety.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to jrcochranusa)
 
 
Post #: 8
 
 RE: AD Query / Script Help / Easy - 1/22/2008 7:17:56 AM   
  jrcochranusa


Posts: 27
Score: 0
Joined: 1/30/2007
Status: offline
This is it... if I can figure out this syntax, I can place this code into my script. This should run on it's own... Thx!

---------------------------------------------------------------------------

On Error Resume Next

strDN = "cn=Cochran\, Jim,ou=usr,ou=it,dc=domain,dc=com"
Set objUser = Getobject("LDAP:// " & strDN & ")
strManagerDisplayName = objUser.displayname

_____________________________

Thanks,

jrc

(in reply to ebgreen)
 
 
Post #: 9
 
 RE: AD Query / Script Help / Easy - 1/22/2008 7:43:08 AM   
  ebgreen


Posts: 4971
Score: 31
Joined: 7/12/2005
Status: offline
change this:

Set objUser = Getobject("LDAP:// " & strDN & ")


to this:

Set objUser = Getobject("LDAP:// " & strDN)

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to jrcochranusa)
 
 
Post #: 10
 
 RE: AD Query / Script Help / Easy - 1/22/2008 8:06:01 AM   
  jrcochranusa


Posts: 27
Score: 0
Joined: 1/30/2007
Status: offline
That worked. Is it me, or is that impossible to find in any reference material? I have two scripting books at my desk and couldn't even find an example of that. I thought for sure that the variable "strDN" would replace exactly what I removed from the equation? I'm kind of a block head...but a really good guitar player.





_____________________________

Thanks,

jrc

(in reply to ebgreen)
 
 
Post #: 11
 
 RE: AD Query / Script Help / Easy - 1/22/2008 8:11:45 AM   
  ebgreen


Posts: 4971
Score: 31
Joined: 7/12/2005
Status: offline
Generally just count the number of "s. With some exceptions it should always be an even number of them on any given line. The same is true for all block delimiters ...(), [], {}, etc.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to jrcochranusa)
 
 
Post #: 12
 
 RE: AD Query / Script Help / Easy - 1/22/2008 8:14:32 AM   
  jrcochranusa


Posts: 27
Score: 0
Joined: 1/30/2007
Status: offline
Awesome! Thanks for all of your help and the tip is duly noted.

_____________________________

Thanks,

jrc

(in reply to ebgreen)
 
 
Post #: 13
 
 RE: AD Query / Script Help / Easy - 1/28/2008 11:17:14 AM   
  jrcochranusa


Posts: 27
Score: 0
Joined: 1/30/2007
Status: offline
I ran into another snag:

I can query AD and return a users manager in DN format. I am then able to query AD and get the managers DisplayName. The problem is that I can't seem to clear the variable "CurManagerDisplayName".

Example: If I have a user and his managers name is "Smith, John", but the next 10 users manager is field "null" or "blank", it  still displays "Smith, John".

I have tried clearing the variable after each record count.

Clear CurManagerDisplayName

or

CurManagerDisplayName = ""

Any help would be greatly appreciated, entire script is below, and the only thing I changed was the domain name.

---------------------------------------------------------------------------------------------

  On error resume Next
  TargetDomain="dc=mydomain,dc=com"
  Set objConnection = CreateObject("ADODB.Connection")
  objConnection.Open "Provider=ADsDSOObject;"
  Set objCommand = CreateObject("ADODB.Command")
  objCommand.ActiveConnection = objConnection
  const ForReading = 1, ForWriting = 2, ForAppending = 8
  q=chr(34)
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set fsOutFile = objFSO.OpenTextFile("Peregrine.CSV", ForWriting, true)
  Set fsAllOutFile = objFSO.OpenTextFile("AllOut.CSV", ForWriting, true)
  Set fsRepFile = objFSO.OpenTextFile("Report.txt", ForWriting, true)
  objCommand.CommandText = _
     "<GC://" & TargetDomain & ">;(&(objectCategory=person)(objectClass=user))" & _
     ";objectCategory,givenName,sn,cn,displayName,sAMAccountName,mailNickname,title," & _
     "department,physicalDeliveryOfficeName,company,streetAddress,l,st,c,postalCode," & _
     "telephoneNumber,manager,facsimileTelephoneNumber,extensionAttribute1,mail," & _
     "extensionAttribute2,distinguishedName,msExchHomeServerName;subtree"
' ************** Execute the LDAP query request *****
  wscript.echo "  ###    Executing...    ###"
  fsRepFile.WriteLine "  ###    Executing...    ###" & vbCrLf
  set objRecordSet = objCommand.Execute
  ADUserCount=0
  TotalFixes=0
' ************* InitializeFile with Header *********
  fsOutFile.WriteLine "SAM,Firstname,Lastname,Full Name,Email Address,Title,Corp/Comp/Dept,Division,Location Code,Phone Number,Manager"
  fsRepFile.WriteLine  "SAM,Firstname,Lastname,Full Name,Email Address,Title,Corp/Comp/Dept,Division,Location Code,Phone Number,Manager" & VbCrLf
  While Not objRecordset.EOF
  recCount=recCount+1
  'wscript.echo recCount, trim(objRecordset.Fields("displayName"))
  'if recCount = 50 then wscript.quit
 
  PrintFlag=0
  FixFlag=0
     CurCat=trim(objRecordset.Fields("objectCategory"))
     CurGN=trim(objRecordset.Fields("givenName"))
     CurSN=trim(objRecordset.Fields("sn"))
     CurName=trim(objRecordset.Fields("displayName"))
     CurSam=trim(objRecordset.Fields("sAMAccountName"))
     CurFirstname=objRecordset.Fields("givenname")
     CurLastname=objRecordset.Fields("sn")
     CurNickname=trim(objRecordset.Fields("mailNickname"))
     CurTitle=trim(objRecordset.Fields("title"))
     CurDept=trim(objRecordset.Fields("department"))
     CurPDOffice=trim(objRecordset.Fields("physicalDeliveryOfficeName"))
     CurComp=trim(objRecordset.Fields("company"))
     CurStreet=trim(objRecordset.Fields("streetAddress"))
     CurCity=trim(objRecordset.Fields("l"))
     CurState=trim(objRecordset.Fields("st"))
     CurCountry=trim(objRecordset.Fields("c"))
     CurZip=trim(objRecordset.Fields("postalCode"))
     CurTele=trim(objRecordset.Fields("telephoneNumber"))
     Curmanager=trim(objRecordset.Fields("manager"))
     CurFax=trim(objRecordset.Fields("facsimileTelephoneNumber"))
     CurEA1=trim(objRecordset.Fields("extensionAttribute1"))
     CurMail=trim(objRecordset.Fields("mail"))
     CurEA2=trim(objRecordset.Fields("extensionAttribute2"))
     TempMSXSvr=trim(objRecordset.Fields("msExchHomeServerName"))
     CurMSXSvr=mid(TempMsxSvr,instr(TempMSXSvr, "cn=Servers/cn=")+14, Len(TempMSXSvr)-instr(TempMSXSvr, "cn=Servers/cn=")+14)
     UserDN=objRecordset.Fields("distinguishedName")
'######  Clean out ASCII Carriage return line feed #######################
     if CurCat<>"" then CurCat=replace(CurCat,chr(13),"")
     if CurGN<>"" then CurGN=replace(CurGN,chr(13),"")
     if CurSN<>"" then CurSN=replace(CurSN,chr(13),"")
     if CurCN<>"" then CurSN=replace(CurSN,chr(13),"")
     if CurName<>"" then CurName=replace(CurName,chr(13),"")
     if CurFirstname<>"" then CurFirstname=replace(CurFirstname,chr(13),"")
     if CurLastname<>"" then CurLastname=replace(CurLastname,chr(13),"")
     if CurSam<>"" then CurSam=replace(CurSam,chr(13),"")
     if CurNickname<>"" then CurNickname=replace(CurNickname,chr(13),"")
     if CurTitle<>"" then CurTitle=replace(CurTitle,chr(13),"")
     if CurDept<>"" then CurDept=replace(CurDept,chr(13),"")
     if CurPDOffice<>"" then CurPDOffice=replace(CurPDOffice,chr(13),"")
     if CurComp<>"" then CurComp=replace(CurComp,chr(13),"")
     if CurStreet<>"" then CurStreet=replace(CurStreet,chr(13),"")
     if CurCity<>"" then CurCity=replace(CurCity,chr(13),"")
     if CurState<>"" then CurState=replace(CurState,chr(13),"")
     if CurCountry<>"" then CurCountry=replace(CurCountry,chr(13),"")
     if CurZip<>"" then CurZip=replace(CurZip,chr(13),"")
     If CurTele<>"" Then CurTele=replace(CurTele,chr(13),"")
     If CurManager<>"" Then CurManager=replace(CurManager,chr(13),"")
     if CurFax<>"" then CurFax=replace(CurFax,chr(13),"")
     if CurEA1<>"" then CurEA1=replace(CurEA1,chr(13),"")
     if CurMail<>"" then CurMail=replace(CurMail,chr(13),"")
     if CurEA2<>"" then CurEA2=replace(CurEA2,chr(13),"")
     if CurMSXSvr<>"" then CurMSXSvr=replace(CurMSXSvr,chr(13),"")
     if CurCat<>"" then CurCat=replace(CurCat,chr(10),"")
     if CurGN<>"" then CurGN=replace(CurGN,chr(10),"")
     if CurSN<>"" then CurSN=replace(CurSN,chr(10),"")
     if CurName<>"" then CurName=replace(CurName,chr(10),"")
     if CurFirstname<>"" then CurFirstname=replace(CurFirstname,chr(10),"")
     if CurLastname<>"" then CurLastname=replace(CurLastname,chr(10),"")
     if CurSam<>"" then CurSam=replace(CurSam,chr(10),"")
     if CurNickname<>"" then CurNickname=replace(CurNickname,chr(10),"")
     if CurTitle<>"" then CurTitle=replace(CurTitle,chr(10),"")
     if CurDept<>"" then CurDept=replace(CurDept,chr(10),"")
     if CurPDOffice<>"" then CurPDOffice=replace(CurPDOffice,chr(10),"")
     if CurComp<>"" then CurComp=replace(CurComp,chr(10),"")
     if CurStreet<>"" then CurStreet=replace(CurStreet,chr(10),"")
     if CurCity<>"" then CurCity=replace(CurCity,chr(10),"")
     if CurState<>"" then CurState=replace(CurState,chr(10),"")
     if CurCountry<>"" then CurCountry=replace(CurCountry,chr(10),"")
     if CurZip<>"" then CurZip=replace(CurZip,chr(10),"")
     If CurTele<>"" Then CurTele=replace(CurTele,chr(10),"")
     If CurManager<>"" Then CurManager=replace(CurManager,chr(10),"")
     if CurFax<>"" then CurFax=replace(CurFax,chr(10),"")
     if CurEA1<>"" then CurEA1=replace(CurEA1,chr(10),"")
     if CurMail<>"" then CurMail=replace(CurMail,chr(10),"")
     if CurEA2<>"" then CurEA2=replace(CurEA2,chr(10),"")
     if CurMSXSvr<>"" then CurMSXSvr=replace(CurMSXSvr,chr(10),"")
'###############################################
' Don't check for problems prior to this area  #
'###############################################
' Use next line to determine if a certain user is being located in AD
'If CurSam="902863" then Wscript.echo Curcat, CurName

' Change listing for CurCat to user if the DN is standard
     if CurCat ="CN=Person,CN=Schema,CN=Configuration,DC=mydomain,DC=COM" then CurCat="User"
    
' Manager property is returned as DN, convert to DisplayName
Set objUser = GetObject ("LDAP://" & CurManager)
CurManagerDisplayName =  objUser.Get("displayname")



'Selection of users based on certain criterion
If isnumeric(CurSam)<>0 then PrintFlag=1
If instr(CurSam,"P0")<>0 then PrintFlag=1
If instr(CurSam,"B0")<>0 then PrintFlag=1
If instr(CurSam,"C0")<>0 then PrintFlag=1
If instr(UCase(CurSam),"SYS")<>0 then PrintFlag=0 'Leave out "SYS" accounts
If instr(UCase(CurSam),"TST")<>0 then PrintFlag=0 'Leave out "TST" accounts
If instr(UCase(CurSam),"US")<>0 then PrintFlag=0 'Leave out "US" accounts
If InStr(CurName,"SystemMailbox")<>0 Then PrintFlag=0 ' Leave out SystemMailboxes
     if CurCat<>"" then
       If instr(Curname,"$") = 0 then
         strTotalLine = q & _
         CurSam & q & "," & q & _
         CurFirstname & q & "," & q & _
         CurLastname & q & "," & q & _
         CurName & q & "," & q & _
         CurMail & q & "," & q & _
         CurTitle & q & "," & q & _
         CurComp & q & "," & q & _
         CurDept & q & "," & q & _
         CurPDOffice & q & "," & q & _
         CurTele & q & "," & q & _
         CurManagerDisplayName & q
         end If
     end If
   
If (PrintFlag=1) And (ADUserCount<30) then Wscript.echo "AD " & AdUserCount & ": " &strTotalLine
    '  ***************************** Write the data to the file.
    If PrintFlag=1 then fsOutFile.WriteLine strTotalLine
     ' ############################# Unrem the next line for everything.  Good when trying to troubleshoot
     'fsAllOutFile.WriteLine strTotalLine
    
' **** Count what you've done.
    ADUserCount=ADUserCount+1
    'if ADuserCount > 6000 then Wscript.Quit ' Troublshooting line
       if ADUserCount/500 = int(ADUserCount/500) then
         wscript.echo "Processed " & ADUserCount & " records..."
       end if
     objRecordset.MoveNext
   Wend
     Wscript.echo "ADUserCount = " & ADUserCount
     fsRepFile.WriteLine  "ADUserCount = " & ADUserCount & vbCrLf
' **** Regular count done.
Wscript.echo "# SCRIPT COMPLETE #"
fsRepFile.WriteLine  "# SCRIPT COMPLETE #" & VbCrLf


_____________________________

Thanks,

jrc

(in reply to jrcochranusa)
 
 
Post #: 14
 
 RE: AD Query / Script Help / Easy - 1/29/2008 1:16:50 AM   
  ebgreen


Posts: 4971
Score: 31
Joined: 7/12/2005
Status: offline
So doing this does not work?

Set objUser = GetObject ("LDAP://" & CurManager)
CurManagerDisplayName = ""
CurManagerDisplayName =  objUser.Get("displayname")


_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to jrcochranusa)
 
 
Post #: 15
 
 RE: AD Query / Script Help / Easy - 1/29/2008 3:39:22 AM   
  jrcochranusa


Posts: 27
Score: 0
Joined: 1/30/2007
Status: offline
No...that didn't work, in fact, the script just kind of got stuck in a loop and didn't provide any data.

I know it's kind of tough cause I think this script is kind of hack...

_____________________________

Thanks,

jrc

(in reply to ebgreen)
 
 
Post #: 16
 
 RE: AD Query / Script Help / Easy - 1/29/2008 3:45:05 AM   
  jrcochranusa


Posts: 27
Score: 0
Joined: 1/30/2007
Status: offline
Shouldn't I be able to clear the variable right after the script writes the current line?



_____________________________

Thanks,

jrc

(in reply to jrcochranusa)
 
 
Post #: 17
 
 RE: AD Query / Script Help / Easy - 1/29/2008 3:48:37 AM   
  ebgreen


Posts: 4971
Score: 31
Joined: 7/12/2005
Status: offline
Remove the On Error Resume Next that is at the top of your script.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to jrcochranusa)
 
 
Post #: 18
 
 RE: AD Query / Script Help / Easy - 1/29/2008 3:50:57 AM   
  jrcochranusa


Posts: 27
Score: 0
Joined: 1/30/2007
Status: offline
C:\peregrine.vbs(26, 4) Provider: Table does not exist.

Which is...

Set objRecordSet = objCommand.Execute

_____________________________

Thanks,

jrc

(in reply to jrcochranusa)
 
 
Post #: 19
 
 RE: AD Query / Script Help / Easy - 1/29/2008 3:53:33 AM   
  ebgreen


Posts: 4971
Score: 31
Joined: 7/12/2005
Status: offline
Sounds like you have a problem then. I think you are trying to start too big. Start small. Start by just connecting to AD and getting one property.

_____________________________

"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick
Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm
http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to jrcochranusa)
 
 
Post #: 20
 
 
Page:   [1] 2   next >   >>
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> AD Query / Script Help / Easy Page: [1] 2   next >   >>
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts