首页 > 解决方案 > 如何使用 VBScript 从 Active Directory 中获取部门信息?

问题描述

我不是任何类型的脚本编写者,只是试图获取一些域信息的网络专家。

我有一个包含多个 OU、多个安全组的域。我正在尝试根据他们是域中特定安全组的成员来填充 AD 中用户帐户的“部门:”字段(在“组织”选项卡下)。

我正在使用的代码在这里:

{
On Error Resume Next

Set objGroup = GetObject _
    ("LDAP://CN=LiveTimeCustomers,OU=Service Accounts,DC=domain,DC=com", group)
' WScript.Echo objGroup.Name

For Each objMember In objGroup.Members
    ' WScript.Echo vbCrlf & "    Name: " & objMember.Name
    arrGroups = objMember.GetEx("memberOf")
    If (Err.Number = 0) Then
        On Error GoTo 0 
          strGroups = LCase(Join(arrGroups))
' Update Department attribute for COMMUNITY SECTION members           

        If InStr(strGroups, "cn=community section,ou=community,ou=organisation,dc=domain,dc=com") Then 
            ' WScript.Echo vbCrlf & "    Name: " & objMember.Name & " is member of COMMUNITY SECTION"
            Set objUser = GetObject(objMember.ADsPath)
            objUser.department = "COMMUNITY SECTION"
            objUser.SetInfo
        End If

}

我在运行时遇到的错误是:

行:23 字符:5 在缓存中找不到目录属性。行和字符指的是这个:arrGroups = objMember.GetEx("memberOf")

标签: vbscript

解决方案


答案是不要在 vbscript 中执行此操作!无论如何感谢您的帮助。在建议将此脚本更新为 Powershell 后,我就这样做了。我需要运行一个“启动”脚本以管理员身份运行 PS,并调用我想要编辑 AD 的脚本。

启动脚本: Start-Process powershell '-NoProfile -File \\domain.com\SYSVOL\domain.com\scripts\SetDepartmentAttribute.ps1' -verb RunAs

活动目录代码:

    `Import-Module ActiveDirectory

Get-ADGroupMember -Identity "COMMUNITY"| Set-ADUser -Replace @{Department="COMMUNITY"}`

所有这一切都提供了您在 DC 上的执行策略允许运行脚本 - 检查一下。


推荐阅读