redbird
Fresh Scripter
Registered: 2002-06-26
Posts: 10
Loc: Illinois State University - No...
|
Howard, again thank you for your prompt reply. I have been reading the bulletin board and looking through much of it and really respect your information. Here is my code:
Dim $mach[1000] Dim $Filter[0] ;Declare filter for Object $Filter[0]="Computer" ;Or replace "Group" with "User" or "Computer" $MyProvider = "LDAP://" ;AD Provider used to search $IniFile = "c:\winnt\myfile.inf" ; Set the login credential $sUser = "Domail\user" $sPassword = "***********" ; Set the domain controler and the proper context $sDomain = "DC=myad,DC=mydomain,DC=edu"
? @WKSTA ;Print Machine Name $mach=ADSearch(@WKSTA) ;Search AD for Machine Name
If $ItemFound $MyPath = $mach[0]
$MyAdsPath = $MyProvider + $MyPath ;Concat for FULL Path to Object ; Connect to ADS with the provided login credential $oProvider = GetObject("LDAP:") $rootDSE = $oProvider.OpenDSObject("LDAP://" + $sDomain + "/RootDSE", $sUser, $sPassword, 1) ? $MyAdsPath ;Print FULL Path $ou = GetObject($MyAdsPath) ;Get Object with full path $ou.Filter = $Filter ? $ou.Name ;Display Object Name cn ? $ou.description ;Display Object description $UserID = $ou.description $CN = SubStr($ou.Name,4) $Tag = SubStr($CN,4) If $UserID <> "" ;Write INI File $result=WriteProfileString($IniFile,'ULID','id',$UserID) $result=WriteProfileString($IniFile,'ULID','cn',$CN) $result=WriteProfileString($IniFile,'ULID','Tag',$Tag) $result=WriteProfileString($IniFile,'ULID','Valid',"True") $result=WriteProfileString($IniFile,'ULID','AdsPath',$MyAdsPath) Else $result=WriteProfileString($IniFile,'ULID','id',"Basic") $result=WriteProfileString($IniFile,'ULID','cn',$CN) $result=WriteProfileString($IniFile,'ULID','Tag',$Tag) $result=WriteProfileString($IniFile,'ULID','Valid',"False") $result=WriteProfileString($IniFile,'ULID','AdsPath',$MyAdsPath) EndIf
Else ;Object not found in AD $result=WriteProfileString($IniFile,'ULID','id',@USERID) $result=WriteProfileString($IniFile,'ULID','cn',@WKSTA) $result=WriteProfileString($IniFile,'ULID','Tag',SubStr(@WKSTA,4)) $result=WriteProfileString($IniFile,'ULID','Valid',"False") $result=WriteProfileString($IniFile,'ULID','AdsPath',"Error - Not Found")
EndIf
Exit
; Function ADSearch() ; ; v1.1 ; ; Author: Heitz Alex (HeitzAlexandre@wanadoo.fr) ; ; Action: Search in Active Directory using LDAP ; ; Syntax: ADSearch("Account Name", "Full Name") ; ; Parameters: Full Name (Optional) ; The full name to search in the AD database ; ; Account Name (Optional) ; The account name (or ID name) to search in the AD database ; ; Dependancies: Active Directory client installed (Build-in in 2000 and XP systems) ; ; Remarks: Working on 9x (95, 98, Me) and NTx (4, 2000, XP) systems ; Account name of a computer has a trailing $ ; Only * wildcard is supported. ; ADSearch can't be designed to only retreive user and computer (ADODB and LDAP limits) ; ; ; Returns: Array of elements containing the complete following string : ; "CN=full name,...rest of DSN...,DC=Domain,DC=Organization,DC=com or org or...,AccountName=account name,IsContainer=0|1, IsGroup=0|1 ; Iscontainer is set to 1 when AccountName is empty ; IsGroup is set to 1 when the current element is a Group ; ; example: ADSearch("Computers") return 1 array with the following value : "CN=Computers,DC=Domain,DC=Organization,DC=com|AccountName=|IsContainer=1|IsGroup=0" ; ADSearch("P*") return X arrays with values like this one : "CN=Username,OU=OU container name,DC=Domain,DC=Organization,DC=com|AccountName=Useraccount|IsContainer=0|IsGroup=0" ; ADSearch(,"*$$") return X arrays with values like this one : "CN=Computer name,CN=Computers,DC=Domain,DC=Organization,DC=com|AccountName=Computer name$|IsContainer=0|IsGroup=0" Function ADSearch(Optional $ADFullName,Optional $ADAccountName) Dim $AdoCon Dim $AdoCommand Dim $Recordset Dim $Filter Dim $i Select Case Len($ADFullName)>0 AND Len($ADAccountName)>0 $filter=" where cn='"+$adfullname+"' and samAccountName='"+$ADAccountName+"' " Case Len($ADFullName)>0 $filter=" where cn='"+$ADFullName+"' " Case Len($ADAccountName)>0 $filter=" where samAccountName='"+$ADAccountName+"' " Case 1 ; I'm sure you don't want to retreive the complete Active Directory database Return EndSelect $AdoCon = CreateObject("ADODB.Connection") $AdoCon.Provider = "ADsDSOObject"
; WAS: Current credentials are used, as username and password aren't specified ;$AdoCon.Open("Active Directory Provider") ;Now I tried to specify credentials here since Local Adminstrator does not have ;access to AD $AdoCon.Open("Active Directory Provider", $sUser, $sPassword)
; Create ADO command object for the connection. $AdoCommand = CreateObject("ADODB.Command") $AdoCommand.ActiveConnection = $AdoCon
$MyCommandText = "Select AdsPath from '" + $MyProvider + $sDomain + "'" + $filter
? $MyCommandText $result=WriteProfileString($IniFile,'ULID','Command',$MyCommandText) $AdoCommand.CommandText =$MyCommandText
; Execute the query. $Recordset = $AdoCommand.Execute If @ERROR=0 ; $Recordset is 1 based If $Recordset.RecordCount>0 ? $recordset.recordcount $ItemFound = $recordset.recordcount ReDim $ADSearch[$recordset.RecordCount-1] $Recordset.movefirst For $i=0 to $recordset.RecordCount-1 ; Here we'll parse the fields of the current record $ADSearch[$i]=SubStr($Recordset.Fields("AdsPath").value,8) ; To get rid of " LDAP://" ; Moving to the next record we have in $Recordset $Recordset.Movenext Next EndIf Else ? @ERROR EndIf EndFunction
|