Excel VBA: 如何从AD组中查找描述

huangapple go评论45阅读模式
英文:

Excel VBA: how to find a description from an AD-group

问题

我已经浏览了这个网站很多次以寻找答案,这是我的第一个问题。这里有一个很棒的社区!

为了一个项目,我需要制作一个Excel表格,根据当前的Active Directory组来建议新的Active Directory组。为此,我需要当前Active Directory组的描述字段(这是我们组织中的强制字段)。

所以我已经有一个可以通过将其标记为颜色来验证组存在性的脚本。这只是一个多步问题的第一步。这并不会检索描述信息。到目前为止,我在互联网上也没有找到一个我理解的有效解决方案。
使问题更复杂的可能是这些组位于不同的容器中,这使得区分名称前缀不一致。

任何帮助将不胜感激。
Jeroen

到目前为止的代码。这会从D列(从D2开始)读取AD组,然后在Active Directory中搜索它。然后,如果找到了组,它会用绿色标记一个单元格。

Sub ValidateGroupName()

Dim objController
Dim objGCController
Dim objConnection
Dim objCommand
Dim strADPath
Dim objRecordSet
Dim objFields

Dim Y As Integer
Dim GroupName As String
Dim ActSheet As String
Dim Descriptionname As String

ActSheet = ActiveSheet.Name

' 设置AD连接

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection

Set objController = GetObject("GC:")

' 从AD获取记录

For Each objGCController In objController
    strADPath = objGCController.ADspath
Next

Y = 0
Do

GroupName = Sheets(ActSheet).Range("D2").Offset(Y, 0).Value

objCommand.CommandText = _
    "<" & strADPath & ">;(&(objectClass=Group)" & _
         "(cn=" & GroupName & "));distinguishedName;subtree"

objCommand.Properties("Page Size") = 50000
Set objRecordSet = objCommand.Execute

' 对结果采取什么措施?
If objRecordSet.RecordCount = 0 Then
    ' 将单元格颜色更改为红色
    Sheets(ActSheet).Range("E2").Offset(Y, 0).Interior.Color = 255
Else
    ' 将单元格颜色更改为绿色
    Sheets(ActSheet).Range("E2").Offset(Y, 0).Interior.Color = 7138816
End If

Y = Y + 1

Loop Until Sheets(ActSheet).Range("D2").Offset(Y, 0).Value = ""

' 关闭AD连接
objConnection.Close

End Sub

希望我走在正确的道路上,但也许有不同的方法可以提供更清洁的解决方案。

英文:

I've browsed this site many times for answers and this is my first question. Great community here!

For a project I need to make an Excel sheet that suggests new Active directory groups based on their current active directory groups. For this I need the description field of the current active directory group (this is a mandatory field in our organization).

So I already have a script that can verify the existence of a Group by marking it with a colour. It's only 1 step of a multi step problem.
This doesn't retrieve the description information. And I have not found a working solution on the internet so far that I also understood.
A problem that might make it more complex is that the groups are in different Containers. This make the distinguished name prefix inconsistent.

Any help would be welcome.
Jeroen

Code so far. This reads ADgroups from column D (starting with D2) and searches it in active directory. Then it marks a cell with the colour green if it was found.

Sub ValidateGroupName()

Dim objController
Dim objGCController
Dim objConnection
Dim objCommand
Dim strADPath
Dim objRecordSet
Dim objFields

Dim Y As Integer
Dim GroupName As String
Dim ActSheet As String
Dim Descriptionname As String

ActSheet = ActiveSheet.Name

&#39; Set up AD connection

Set objConnection = CreateObject(&quot;ADODB.Connection&quot;)
    objConnection.Open &quot;Provider=ADsDSOObject;&quot;

Set objCommand = CreateObject(&quot;ADODB.Command&quot;)
    objCommand.ActiveConnection = objConnection

Set objController = GetObject(&quot;GC:&quot;)

&#39; Get record from AD

For Each objGCController In objController
    strADPath = objGCController.ADspath
    &#39;strADDescription = objGCController.ADspath
Next


Y = 0
Do

GroupName = Sheets(ActSheet).Range(&quot;D2&quot;).Offset(Y, 0).Value

    objCommand.CommandText = _
    &quot;&lt;&quot; &amp; strADPath &amp; &quot;&gt;;(&amp;(objectClass=Group)&quot; &amp; _
         &quot;(cn=&quot; &amp; GroupName &amp; &quot;));distinguishedName;subtree&quot;
         

objCommand.Properties(&quot;Page Size&quot;) = 50000
Set objRecordSet = objCommand.Execute
    
&#39; What to do with results?
If objRecordSet.RecordCount = 0 Then
&#39;change color of a cell to red
Sheets(ActSheet).Range(&quot;E2&quot;).Offset(Y, 0).Interior.Color = 255
Else
&#39; change color of a cell to green
Sheets(ActSheet).Range(&quot;E2&quot;).Offset(Y, 0).Interior.Color = 7138816
End If

Y = Y + 1

Loop Until Sheets(ActSheet).Range(&quot;D2&quot;).Offset(Y, 0).Value = &quot;&quot;

&#39; Close AD connection
    objConnection.Close

End Sub

I hope I'm on the right track, but a different approach might be a cleaner solution.

答案1

得分: 0

第一步,将 'description' 属性添加到您的查询中:

objCommand.CommandText = _
    "&lt;" & strADPath & "&gt;;(&amp;(objectClass=Group)" & _
    "(cn=" & GroupName & "));distinguishedName;subtree;Description"

第二步,如果群组存在,则获取属性值,并将其写在群组单元格旁边,例如:

If objRecordSet.RecordCount = 0 Then
    '将单元格颜色更改为红色
    Sheets(ActSheet).Range("E2").Offset(Y, 0).Interior.Color = 255
Else
    '将单元格颜色更改为绿色
    Sheets(ActSheet).Range("E2").Offset(Y, 0).Interior.Color = 7138816
    Sheets(ActSheet).Range("E2").Offset(Y, 1).Text = objRecordset.Fields("Description")
End If
英文:

first, add 'description' property to your query :

objCommand.CommandText = _
    &quot;&lt;&quot; &amp; strADPath &amp; &quot;&gt;;(&amp;(objectClass=Group)&quot; &amp; _
         &quot;(cn=&quot; &amp; GroupName &amp; &quot;));distinguishedName;subtree;Description&quot;

second, get property value if group exist and write it beside group cell for example :

If objRecordSet.RecordCount = 0 Then
&#39;change color of a cell to red
Sheets(ActSheet).Range(&quot;E2&quot;).Offset(Y, 0).Interior.Color = 255
Else
&#39; change color of a cell to green
Sheets(ActSheet).Range(&quot;E2&quot;).Offset(Y, 0).Interior.Color = 7138816
Sheets(ActSheet).Range(&quot;E2&quot;).Offset(Y, 1).text = objRecordset.Fields(&quot;Description&quot;)
End If

huangapple
  • 本文由 发表于 2023年2月16日 16:58:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75469849.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定