Arend_
(MM club member)
2006-02-17 08:46 AM
AD Scripting User Information

Since it can be somewhat of a pain to find out all of the user's properties I thought I'd make a list of what's most common. If anyone has anymore properties they'd like to see just make a post and I'll add it.

Code:

$username = "testusername"
$userhome = TranslateName (3, "", 3, "@LDomain\$username", 1)
$userinfo = GetObject("LDAP://" + $userhome[0])

? "General Info"
? "------------"
? " "
? "First Name: " + $userinfo.givenName
? "Initials: " + $userinfo.initials
? "Last Name: " + $userinfo.sn
? "Full Name: " + $userinfo.FullName
? "Display Name: " + $userinfo.displayName
? "Account Name: " + $userinfo.sAMAccountName
? "Distinguished Name: " + $userinfo.distinguishedName
? "Description: " + $userinfo.Description
? "Office Location: " + $userinfo.physicalDeliveryOfficeName
? "Email: " + $userinfo.mail
? "Web Page: " + $userinfo.wwwHomePage
? "Street: " + $userinfo.streetAddress
? "Postal Code: " + $userinfo.postalCode
? "Post Office Box: " + $userinfo.postOfficeBox
? "City: " + $userinfo.l
? "State or Province: " + $userinfo.st
? "Country or Region: " + $userinfo.co
? "Home Phone: " + $userinfo.homePhone
? "Pager: " + $userinfo.pager
? "Mobile Phone: " + $userinfo.mobile
? "Telephone Number: " + $userinfo.telephoneNumber
? "Fax Number: " + $userinfo.facsimileTelephoneNumber
? "Notes: " + $userinfo.info
? "Title: " + $userinfo.title
? "Department: " + $userinfo.department
? "Company Name: " + $userinfo.company
? "Principal Name: " + $userinfo.userPrincipalName
? " "
? "Profile Info"
? "------------"
? " "
? "Profile Path: " + $userinfo.profilePath
? "Script Path: " + $userinfo.scriptPath
? "Home Directory: " + $userinfo.homeDirectory
? "Home Drive: " + $userinfo.homeDrive
? "Terminal Services Profile Path: " + $userinfo.TerminalServicesProfilePath
? "Terminal Services Local Path: " + $userinfo.TerminalServicesHomeDirectory
? "Terminal Services Home Drive: " + $userinfo.TerminalServicesHomeDrive
? "Terminal Services Allowed: " + $userinfo.AllowLogon
? " "
? "Account Info"
? "------------"
? " "
? "User Account Control: " + $userinfo.userAccountControl
? "Account Disabled: " + $userinfo.AccountDisabled
? "Account Locked: " + $userinfo.IsAccountLocked
? "Account Created: " + $userinfo.whenCreated
? "Account Last Modified: " + $userinfo.whenChanged
? "Account Expires: " + $userinfo.AccountExpirationDate
? "Last Login: " + $userinfo.LastLogin
? "Last Failed Login: " + $userinfo.LastFailedLogin
? "Logon Count: " + $userinfo.logonCount
? "Bad Login Count: " + $userinfo.BadLoginCount
? "Password Last Changed: " + $userinfo.PasswordLastChanged

; TranslateName function authored by Howard A. Bullock
Function TranslateName ($InitType, $BindName, $LookupNameType, $LookupName, $ReturnNameType)
Dim $InitType, $BindName, $LookupNameType, $LookupName, $ReturnNameType
Dim $NameTranslate, $ReturnName, $Error, $ErrorText
$Error = 0
$ErrorText = ""
$ReturnName = ""
$NameTranslate = CREATEOBJECT ("NameTranslate")
$Error = @error
$ErrorText = @serror
if $Error = 0
$NameTranslate.Init ($InitType, $BindName)
$Error = @error
$ErrorText = @serror
if $Error = 0
$NameTranslate.Set ($LookupNameType, $LookupName)
$Error = @error
$ErrorText = @serror
if $Error = 0
$ReturnName = $NameTranslate.Get($ReturnNameType)
$Error = @error
$ErrorText = @serror
endif
endif
endif
$TranslateName = $ReturnName, $Error, $ErrorText
Endfunction



NTDOCAdministrator
(KiX Master)
2006-02-17 10:08 AM
Re: AD Scripting User Information

Here are some links to get more information on the AD.


Administration and Management
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnanchor/html/setupsysadmin.asp

Active Directory Glossary
http://www.microsoft.com/windows2000/techinfo/howitworks/activedirectory/glossary.asp

Binding to Active Directory
http://msdn.microsoft.com/library/defaul...e_directory.asp

Syntaxes for Active Directory Attributes
http://msdn.microsoft.com/library/defaul..._attributes.asp

All Attributes - The following attributes are defined by Active Directory.
http://msdn.microsoft.com/library/defaul...ributes_all.asp

Mappings for the Active Directory Users and Computers Snap-in
http://msdn.microsoft.com/library/defaul...ers_snap-in.asp


Radimus
(KiX Supporter)
2006-02-17 10:11 AM
Re: AD Scripting User Information

http://www.kixtart.org/ubbthreads/showfl...part=2&vc=1

NTDOCAdministrator
(KiX Master)
2006-02-17 10:14 AM
Re: AD Scripting User Information

Well this link has much more information on each attribute than just it's name as in that post.

All Attributes - The following attributes are defined by Active Directory.
http://msdn.microsoft.com/library/defaul...ributes_all.asp


NTDOCAdministrator
(KiX Master)
2006-02-17 10:15 AM
Re: AD Scripting User Information

Dang 04:00 AM for you Rad, glad I don't work where you do, only 01:00 here and haven't gone to bed yet.

Richard H.Administrator
(KiX Supporter)
2006-02-17 11:26 AM
Re: AD Scripting User Information

You could enumerate the properties. It's not as nicely categorised as your example, but it does pick up all the attributes and saves a lot of typing.

I left out the code to recursively expand objects and arrays as an exercise for the reader.

Code:
Break ON
$=SetOption("WrapAtEOL","ON")

$sUserName = "rhowarth"
$asUserHome = TranslateName (3, "", 3, @LDomain+"\"+$sUserName, 1)
$oUserInfo = GetObject("LDAP://" + $asUserHome[0])
$oUserClass = GetObject($oUserInfo.schema)

$iMaxLen=0
$sPropertyList=""

; Collate properties and determine max length to tidy up the display
For Each $sProperty in $oUserClass.MandatoryProperties
If Len($sProperty)>$iMaxLen $iMaxLen=Len($sProperty) EndIf
$sPropertyList=$sPropertyList+@CRLF+$sProperty
Next
For Each $sProperty in $oUserClass.OptionalProperties
If Len($sProperty)>$iMaxLen $iMaxLen=Len($sProperty) EndIf
$sPropertyList=$sPropertyList+@CRLF+$sProperty
Next

$iMaxLen=$iMaxLen+1
While Len($sSpacer)<$iMaxLen $sSpacer=$sSpacer+" " Loop

; Sort and enumerate the properties
For Each $sProperty in QS(Split(SubStr($sPropertyList,3),@CRLF))
If Instr($sProperty,"-") ; Skip properties that will cause KiXtart parser problems
Left($sProperty+$sSpacer,$iMaxLen)+"N/A"+@CRLF
Else
$=Execute("$$sVarType=VarTypeName($$oUserInfo."+$sProperty+")")
$=Execute("$$sVarValue=$$oUserInfo."+$sProperty)
Left($sProperty+$sSpacer,$iMaxLen)+Left($sVarType+$sSpacer,11)
Select
Case $sVarType="String"
$sVarValue
Case $sVarType="long"
$sVarValue
EndSelect
@CRLF
EndIf
Next

Exit 0

; TranslateName function authored by Howard A. Bullock
Function TranslateName ($InitType, $BindName, $LookupNameType, $LookupName, $ReturnNameType)
Dim $InitType, $BindName, $LookupNameType, $LookupName, $ReturnNameType
Dim $NameTranslate, $ReturnName, $Error, $ErrorText
$Error = 0
$ErrorText = ""
$ReturnName = ""
$NameTranslate = CREATEOBJECT ("NameTranslate")
$Error = @error
$ErrorText = @serror
if $Error = 0
$NameTranslate.Init ($InitType, $BindName)
$Error = @error
$ErrorText = @serror
if $Error = 0
$NameTranslate.Set ($LookupNameType, $LookupName)
$Error = @error
$ErrorText = @serror
if $Error = 0
$ReturnName = $NameTranslate.Get($ReturnNameType)
$Error = @error
$ErrorText = @serror
endif
endif
endif
$TranslateName = $ReturnName, $Error, $ErrorText
Endfunction

; BrianTX's Qsort.
function qs($a)
DIM $b[32],$c[32],$d,$e,$f,$g,$h,$i,$j,$k,$l
$b[0]=0
$c[0]=UBOUND($a)
$d=0
While $d >=0
$e=$b[$d]
$f=$c[$d]
While $e < $f
$h=$e+($f-$e)/2
$k=$a[$e]
$A[$e]=$A[$h]
$A[$h]=$k
$i=$e+1
$j=$f
$l=0
Do
While ($i<$j) AND $A[$e] > $A[$i]
$i=$i+1
Loop
While ($j>=$i) AND $A[$j] > $A[$e]
$j=$j-1
Loop
IF $i>=$j
$l=1
ELSE
$k=$A[$i]
$A[$i]=$A[$j]
$A[$j]=$k
$j=$j-1
$i=$i+1
ENDIF
Until $l=1
$k=$a[$e]
$a[$e]=$a[$j]
$a[$j]=$k
$g=$j
If $g-$e <= $f - $g
If $g+1 < $f
$b[$d]=$g+1
$c[$d]=$f
$d=$d+1
Endif
$f=$g-1
Else
If $g-1 > $e
$b[$d]=$e
$c[$d]=$g-1
$d=$d+1
Endif
$e=$g+1
Endif
Loop
$d=$d-1
Loop
$qs=$a
Endfunction



Chris S.
(MM club member)
2006-02-17 02:27 PM
Re: AD Scripting User Information

You could also use the AD Schema Browser I wrote to look at the property names (and the type of data they return) for every object in the schema.

http://www.kixtart.org/ubbthreads/showfl...true#Post146018


Richard H.Administrator
(KiX Supporter)
2006-02-17 03:04 PM
Re: AD Scripting User Information

You just need to update it so the user can load an instance of the object and display the data

Arend_
(MM club member)
2006-02-17 04:20 PM
Re: AD Scripting User Information

NTDOC: Your last link doesn't work
For the rest, thanks for contibuting, this is exactly what I intended. With this thread every user looking for AD user info will get all the info they will need. Thank you all


NTDOCAdministrator
(KiX Master)
2006-02-17 09:35 PM
Re: AD Scripting User Information

Not sure why but the board messed up the link. It was just a repost of one of the above links. All fixed now.

Arend_
(MM club member)
2006-12-15 02:14 PM
Re: AD Scripting User Information

I needed all properties written to a text file so first I started editing Richard's code for my private use but ended up rewriting the whole thing.
It's a bit messy, especially with the tab spaced WriteLog function.
But it works fine and the end result looks nice and this way you know ALL the users properties,types and values whether you have exchange or not you get the properties that are available to you.

Anyway without further ado....
Code:
Break ON
$=SetOption("WrapAtEOL","ON")

$usr = "apronk"
$logf = "D:\Schema.txt"

$cnusr = TranslateName($usr)
$usrnfo = GetObject("LDAP://" + $cnusr)
$usrclas = GetObject($usrnfo.schema)

$fso = CreateObject("Scripting.FileSystemObject")
$log = $fso.OpenTextFile($logf, 8, 1)

$log.WriteLine("Mandatory Properties:")
$log.WriteLine("---------------------")
$log.WriteLine("")

For Each $prop in $usrclas.MandatoryProperties
  If NOT Instr($prop,"-")
    $= Execute("$$Type=VarTypeName($$usrnfo."+$prop+")")
    $= Execute("$$Value=$$usrnfo."+$prop)
    If $type <> "Object" AND $type <> "Variant[]"
      WriteLog($prop,$type,$value)
    Else
      WriteLog($prop,$type)
    EndIf
  Else
    WriteLog($prop,"N/A")
  EndIf
Next

$log.WriteLine("")
$log.WriteLine("Optional Properties:")
$log.WriteLine("--------------------")
$log.WriteLine("")

For Each $prop in $usrclas.OptionalProperties
  If NOT Instr($prop,"-")
    $= Execute("$$Type=VarTypeName($$usrnfo."+$prop+")")
    $= Execute("$$Value=$$usrnfo."+$prop)
    If $type <> "Object" AND $type <> "Variant[]"
      WriteLog($prop,$type,$value)
    Else
      WriteLog($prop,$type)
    EndIf
  Else
    WriteLog($prop,"N/A")
  EndIf
Next

$log.Close

Function WriteLog($LineToWrite,$sType,Optional $sValue)
  If LEN($LineToWrite) < 8
    $log.WriteLine($LineToWrite + "					" + $sType + "		" + $sValue)
  EndIf
  If LEN($LineToWrite) >= 8 AND LEN($LineToWrite) < 16
    $log.WriteLine($LineToWrite + "				" + $sType + "		" + $sValue)
  EndIf
  If LEN($LineToWrite) >= 16 AND LEN($LineToWrite) < 24
    $log.WriteLine($LineToWrite + "			" + $sType + "		" + $sValue)
  EndIf
  If LEN($LineToWrite) >= 24 AND LEN($LineToWrite) < 32
    $log.WriteLine($LineToWrite + "		" + $sType + "		" + $sValue)
  EndIf
  If LEN($LineToWrite) >= 32
    $log.WriteLine($LineToWrite + "	" + $sType + "		" + $sValue)
  EndIf
EndFunction

Function TranslateName($NameToTranslate)
  Dim $NameTranslate
  $NameTranslate = CreateObject("NameTranslate")
  $NameTranslate.Init(3,"")
  $NameTranslate.Set(3, @LDOMAIN + "\" + $NameToTranslate)
  $TranslateName = $NameTranslate.Get(1)
EndFunction



LonkeroAdministrator
(KiX Master Guru)
2006-12-15 04:06 PM
Re: AD Scripting User Information

could change your writelog to something like:
Code:
Function WriteLog($LineToWrite,$sType,Optional $sValue)
    $log.WriteLine($LineToWrite + "	" + 
     left(chr(9)+chr(9)+chr(9)+chr(9),(33>LEN($LineToWrite))*(32-LEN($LineToWrite))/8) +
     $sType + "		" + $sValue)
EndFunction


Arend_
(MM club member)
2006-12-15 05:38 PM
Re: AD Scripting User Information

It was never meant to be clean. It's quick and dirty and it works, thats all. If I clean it up I'll submit it as a totally clean script to the UDF section and then I'll surely implement your idea.

But for now it works fine as is.


LonkeroAdministrator
(KiX Master Guru)
2006-12-16 02:17 AM
Re: AD Scripting User Information

is there something left for cleaning in my version still? ;\)

Wasim
(Fresh Scripter)
2007-03-28 05:40 PM
Re: AD Scripting User Information

hi i m trying to use this

Break ON
"Home Directory: " + $userinfo.homeDirectory
"Home Drive: " + $userinfo.homeDrive

but its not giving me any information am i doing something wrong? (win xp os)


Howard Bullock
(KiX Supporter)
2007-03-28 07:45 PM
Re: AD Scripting User Information

You need to bind to an AD object before you can access object properties.

This is an old thread. Maybe you should start your own thread.