http://dieseyer.de • all rights reserved • © 2011 v11.4

'*** v8.2 *** www.dieseyer.de *******************************
'
' Datei: pc-im-ad.vbs
' Autor: dieseyer@gmx.de
' Auf: www.dieseyer.de
'
' Gibt den 'distinguishedName' eines PCs zurück. Das AD wird
' dazu nicht mit einer For..Each-Schleife durchlaufen, bis
' der PC gefunden wird, sonder es wird 'direkt' gefragt.
'
'************************************************************

Option Explicit ' Siehe http://dieseyer.de/dse-wsh-lernen.html#OptionExpl

Dim Tst, Txt
Dim StartZeit

Dim WSHNet : Set WSHNet = WScript.CreateObject("WScript.Network")
Dim PC : PC = WSHNet.ComputerName

Dim AD
AD = "LDAP://ou=HR,dc=NA,dc=fabrikam,dc=com"
AD = "LDAP://dc=fabrikam,dc=com"

Txt = PCimAD( AD, PC ) ' Prozedur-Aufruf'
Txt = LCase( Txt )
Txt = Replace( Txt, "ldap://", "" )
Txt = Replace( Txt, "cn=", "" )
Txt = Replace( Txt, "ou=", "" )
Txt = Replace( Txt, "dc=", "" )
Txt = Replace( Txt, ",", "." )

MsgBox "AD-Objekt: " & Txt

' StartZeit = Timer() : Tst = "PC13-08" : Txt = PCimAD( AD, Tst ) : MsgBox "Dauer: " & Timer() - Startzeit & vbCRLF & Tst & vbCRLF & PCimAD( AD, Tst ), , "33 :: "
' StartZeit = Timer() : Tst = "PC0815" : Txt = PCimAD( AD, Tst ) : MsgBox "Dauer: " & Timer() - Startzeit & vbCRLF & Tst & vbCRLF & PCimAD( AD, Tst ), , "34 :: "
' StartZeit = Timer() : Tst = "PC-GIBTSNICHT" : Txt = PCimAD( AD, Tst ) : MsgBox "Dauer: " & Timer() - Startzeit & vbCRLF & Tst & vbCRLF & PCimAD( AD, Tst ), , "35 :: "

WScript.Quit



'*** v8.2 *** www.dieseyer.de *******************************
Function PCimAD( AD, PCname )
'************************************************************
' On Error Resume Next

Const ADS_SCOPE_SUBTREE = 2
Dim objConnection, objCommand, objRecordSet
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 '" & AD & "' WHERE objectCategory='Computer' AND Name='" & PCname & "'"
Set objRecordSet = objCommand.Execute

PCimAD = PCname & " ist nicht im AD vorhanden."
On Error Resume Next
PCimAD = objRecordSet.Fields("distinguishedName").Value
On Error GoTo 0
' MsgBox "i = " & i & vbCRLF & "n = " & n & vbCRLF & "Dauer: " & Timer() - StartZeit & vbCRLF & objRecordSet.Fields("distinguishedName").Value , , "63 :: " ' : WScript.Quit

Set objRecordSet = nothing
Set objCommand.ActiveConnection = nothing
Set objCommand = nothing
objConnection.Close
Set objConnection = nothing

End Function ' PCimAD( AD, PCname )


http://dieseyer.de • all rights reserved • © 2011 v11.4