2015-10-08 30 views
0

我需要一個基於多個DN的Active Directory影子組(又名Active Directory動態組)。如何將主機名和憑證提供給「LDAP://」?

我搜索了一個簡單的工具,可以讓我做到這一點,並最終在http://kb.caresys.com.cn/4052785/need-script-add-all-accounts-active-directory-security-group(和其他幾個地方)找到了Dan Holme的優秀腳本(以下引用) 我還發現了幾個PowerShell腳本,但它們似乎都具有更強大的依賴性,我需要一個儘可能獨立的工具。他們也都遇到了我在這裏遇到的同樣的問題。

的Group_Shadow.vbs腳本執行正是我需要有一個例外: 我需要能夠指定廣告的主機,端口號和憑據(用戶名和密碼)。

腳本假設"LDAP://"指向正確的廣告,我猜AD憑據運行腳本的用戶得到的?

通過將"LDAP://"字符串更改爲"LDAP://LDAP_HOST:LDAP_PORT/",我確實找到了有關如何設置主機名和密碼的提示。 這似乎很容易實現 - 但也有一些意見,說明它沒有工作......

我還發現一個提示有關設置憑據:

Dim LDAP ' As IADsOpenDSObject 
Set LDAP = GetObject("LDAP:") 
Set obj = LDAP.OpenDSObject("LDAP://", "domain\name", "password", ADS_USE_ENCRYPTION OR ADS_SECURE_AUTHENTICATION) 

這似乎是難部分(在VBScript和Active Directory世界中完全是新手),而我根本無法弄清楚如何合併爲兩個。

我希望社區能夠幫助我,幫助修復此腳本或指向不同的解決方案。

在此先感謝!

腳本:

'========================================================================== 
' 
' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 2007 
' 
' NAME: Group_Shadow.vbs 
' 
' AUTHOR: Dan Holme , Intelliem 
' DATE : 12/12/2007 
' 
' USAGE: 
' cscript.exe Group_Shadow.vbs 
' 
' Dynamically updates the membership of a group 
' to match the objects returned from an Active Directory query 
' 
' See the Windows Administration Resource Kit for documentation 
' 
' Neither Microsoft nor Intelliem guarantee the performance 
' of scripts, scripting examples or tools. 
' 
' See www.intelliem.com/resourcekit for updates to this script 
' 
' (c) 2007 Intelliem, Inc 
'========================================================================== 
Option Explicit 

Dim sDomainDN 
Dim sGroupSAMAccountName 
Dim aSearchOUs 
Dim sQuery 

'========================================================================== 
' CONFIGURATION BLOCK 
' Domain's DN 
sDomainDN = "dc=domain,dc=local" 
' sAMAccountName of shadow group 
sGroupSAMAccountName = "Security Group" 
' An array of one or more OUs to search 
aSearchOUs = Array("ou=Something,dc=domain,dc=local") 
' LDAP query that will be run in each OU 
sQuery = " (&(objectCategory=computer)(name=GA*));distinguishedName;subtree" 
'========================================================================== 

' Create dictionaries 
Dim dResults 
Set dResults = CreateObject("Scripting.Dictionary") 
dResults.CompareMode = vbTextCompare ' Case INsensitive 
Dim dTargetMembership 
Set dTargetMembership = CreateObject("Scripting.Dictionary") 
dTargetMembership.CompareMode = vbTextCompare ' Case INsensitive 
Dim dCurrentMembership 
Set dCurrentMembership = CreateObject("Scripting.Dictionary") 
dCurrentMembership.CompareMode = vbTextCompare ' Case INsensitive 
Dim dMembershipChanges 
Set dMembershipChanges = CreateObject("Scripting.Dictionary") 
dMembershipChanges.CompareMode = vbTextCompare ' Case INsensitive 

' Perform LDAP searches, adding to final list stored in dTargetMembership 
Dim sSearchOU 
Dim sLDAPQuery 
For Each sSearchOU In aSearchOUs 
    sLDAPQuery = "<LDAP://" & sSearchOU & ">;" & sQuery 
    Set dResults = AD_Search_Dictionary(sLDAPQuery) 
    Call DictionaryAppend(dResults, dTargetMembership) 
Next 

' Locate group 
Dim sGroupADsPath 
Dim oGroup 
sGroupADsPath = ADObject_Find_Generic(sGroupSAMAccountName, sDomainDN) 
If sGroupADsPath = "" Then 
    ' Error handling: group not found 
    WScript.Quit 
End If 
Set oGroup = GetObject(sGroupADsPath) 

' Get members and store in dictionary 
Dim aMembers 
aMembers = oGroup.GetEx("member") 
Set dCurrentMembership = ArrayToDictionary(aMembers) 

' Calculate the "delta" between the current and desired state 
Set dMembershipChanges = Dictionary_Transform(dCurrentMembership, dTargetMembership) 

' Make the membership changes based on the transform dictionary's instructions 
Dim sMember 
For Each sMember In dMembershipChanges 
    If UCase(dMembershipChanges.Item(sMember)) = "ADD" Then 
     oGroup.Add "LDAP://" & sMember 
    End If 
    If UCase(dMembershipChanges.Item(sMember)) = "DELETE" Then 
     oGroup.Remove "LDAP://" & sMember 
    End If 
Next 

WScript.Quit 

' ====================== 
' FUNCTIONS FROM LIBRARY 
' ====================== 

' #region Dictionary routines 

Function ArrayToDictionary(ByRef aArray) 
    ' Converts a one-dimensional array into a dictionary. 
    ' Assumes elements in array are unique 
    Dim dDic 
    Dim aElement 
    Set dDic = CreateObject("Scripting.Dictionary") 
    dDic.CompareMode = vbTextCompare ' Case INsensitive 

    On Error Resume Next ' trap duplicate array elements 
    For Each aElement In aArray 
     dDic.Add aElement, 0   
    Next 
    On Error GoTo 0 

    Set ArrayToDictionary = dDic 
End Function 

Sub DictionaryAppend(ByRef dNewElements, ByRef dDictionary) 
    ' Appends the elements of dNewElements to dDictionary 
    Dim sKey 

    On Error Resume Next ' trap duplicate array elements 
    For Each sKey In dNewElements.keys 
     dDictionary.Add sKey, dNewElements.Item(sKey) 
    Next 
    On Error GoTo 0 
End Sub 

Function Dictionary_Transform(ByVal dOriginal, ByVal dFinal) 
    ' Retunrs a dictionary with a list of update operations required 
    ' so that dOriginal is transformed to dFinal 

    Dim dTransform, sKey 
    Set dTransform = CreateObject("Scripting.Dictionary") 
    dTransform.CompareMode = vbTextCompare ' Case INsensitive 

    For Each sKey In dFinal.Keys 
     If Not dOriginal.Exists(sKey) Then 
      dTransform.Add sKey, "ADD" 
     End If 
    Next 

    For Each sKey In dOriginal.Keys 
     If Not dFinal.Exists(sKey) Then 
      dTransform.Add sKey, "DELETE" 
     End If 
    Next 

    Set Dictionary_Transform = dTransform 

End Function 

' #endregion 

' #region Active Directory object find routines 

Function ADObject_Find_Generic(ByVal sObject, ByVal sSearchDN) 
    ' Version 071130 
    ' Takes any input (name, DN, or ADsPath) of a user, computer, or group, and 
    ' returns the ADsPath of the object as a way of validating that the object exists 
    ' 
    ' INPUTS: sObject     DN or ADsPath to an object 
    '         sAMAccountName (pre-Windows 2000 logon name) of a user or group 
    '         computer name of a computer 
    '   sSearchDN    the DN within which to search (often, the DN of the domain, e.g. dc=contoso, dc=com) 
    ' 
    ' RETURNS: ADObject_Find_Generic ADsPath (LDAP://...) of the object 
    '         blank if object was not found 
    ' 
    ' NOTES: ASSUMPTION: computers, users & groups have unique names. See note inline. 
    ' 
    ' REQUIRES AD_Search_Array routine 
    '   AD_Search_RS routine 
    '   ADObject_Validate routine 

    Dim aResults, sLDAPQuery 
    Select Case ADObject_NameType(sObject) 
     Case "" 
      ADObject_Find_Generic = "" 
     Case "adspath" 
      ADObject_Find_Generic = ADObject_Validate(sObject) 
     Case "distinguishedname" 
      ADObject_Find_Generic = ADObject_Validate("LDAP://" & sObject) 
     Case "name" 
       ' Assumption: No computer has the same name as a user's or group's sAMAccountName 
       ' otherwise, this query will return more than one result 
       sLDAPQuery = "<LDAP://" & sSearchDN & ">;" & _ 
          "(|(samAccountName=" & sObject & ")(samAccountName=" & sObject & "$));" & _ 
          "aDSPath;subtree" 
       aResults = AD_Search_Array (sLDAPQuery) 
       If Ubound(aResults) = -1 Then 
        ADObject_Find_Generic = "" 
       Else 
        ADObject_Find_Generic = aResults(0) 
       End If 
    End Select 

End Function 

Function ADObject_NameType(ByVal sObjectName) 
    ' Version 071204 
    ' Evaluates sObjectName to determine what type of name it is 
    ' Returns ADObject_NameType adspath 
    '        distinguishedname 
    '        name 
    '        blank if sObjectName = "" 

    Dim sNameType 

    If Len(sObjectName) = 0 Then 
     sNameType = "" 

    ElseIf Len(sObjectName) < 3 Then 
     ' can't be a DN or an ADsPath - must be a name 
     sNameType = "name" 

    ElseIf Ucase(Left(sObjectName,3)) = "CN=" Then 
     ' is a DN 
     sNameType = "distinguishedname" 

    ElseIf Len(sObjectName) < 8 Then 
     ' too short to be an ADsPath and isn't a DN, so it must be a name 
     sNameType = "name" 

    ElseIf UCase(Left(sObjectName, 7)) = "LDAP://" Then 
     ' is already an ADsPath 
     sNameType = "adspath" 

    Else 
     ' must be a name 
     sNameType = "name" 

    End If 

    ADObject_NameType = sNameType 
End Function 

Function ADObject_Validate(ByVal sObjectADsPath) 
    ' Version 071122 
    ' Returns ADsPath of object as a way of validating that the object exists 
    ' 
    ' INPUTS: sObjectADsPath  ADsPath of object to test 
    ' RETURNS: ADObject_Validate Path of object (if it exists) or blank 

    Dim oObject 
    On Error Resume Next 
    Set oObject = GetObject(sObjectADsPath) 
    If Err.Number <> 0 Then 
     ADObject_Validate = "" 
     Err 
+0

hostname跟在'//'後面,就像其他URL一樣。無法幫助您獲取憑據。 – EJP

+0

@EJP謝謝。這就說得通了。 – fsteff

回答

0

原來有兩個答案採取的關於音符「LDAP://」證書。我只是不得不睜開我的眼睛! 幾乎腳本的最後一行已經有選項,添加憑據:

oConnection.Open "", vbNullString, vbNullString 

只需必須正確填充:

oConnection.Open "", "username", "password" 

,更一般的描述已經提供@Harvey Kwok在這個SO回答:Secure LDAP object manipulation with VBscript using alternate credentials