用于修改多值Active Directory显示说明符的VBS脚本

时间:2009-06-22 20:41:48

标签: vbscript active-directory

遵循howto Extending the Active Directory Schema To Track Custom Info我能够设置单值模式属性,该属性可以通过ADUC中的上下文菜单轻松更改。多值模式属性变得相当复杂。说(为了争论)我的价值是“项目”,每个用户可以根据需要列出许多项目。

以下是一个令人遗憾的小脚本,它将Project设置为单个值:

Dim oproject
Dim oUser1
Dim temp1
Set oproject = Wscript.Arguments
Set oUser1 = GetObject(oproject(0))
temp1 = InputBox("Project: " & oUser1.project & vbCRLF & vbCRLF & "Project")
if temp1 <> "" then oUser1.Put "project",temp1
oUser1.SetInfo
Set oUser1 = Nothing
Set oproject = Nothing
Set temp1 = Nothing
WScript.Quit

如何修改此选项以允许,分配和修改多个值?

1 个答案:

答案 0 :(得分:1)

我放弃了优雅的用户界面,只是使用了分号分隔列表。如果有人关心,这是代码:

Dim objProject
Dim objUser
Dim temp1, title, message, default
Dim projects
title = "Projects"

Set objProject = Wscript.Arguments
Set objUser = GetObject(objProject(0))

'Find our current projects
projects = objUser.projects
If Not isArray(projects) Then
    projects = Array(projects)
End If

'Setup our message box
message = "Semicolon-delimited list of Projects"
default = arrayToStr(projects)
temp1 = InputBox(message, title, default)

'catch cancels
if IsEmpty(temp1) Then
    WScript.Quit
End If

' update our data
projects = strToArray(temp1)
objUser.Put "projects",projects
objUser.SetInfo

'Clean up and quit
Set projects = Nothing
Set objUser = Nothing
Set objProject = Nothing
Set temp1 = Nothing
Set title = Nothing
Set message = Nothing
Set default = Nothing
WScript.Quit

'Functions
Function strToArray(s)
    Dim a
    Dim token

    ' discard blank entries
    For Each token in split(s, ";")
        token = trim(token)
        If token <> "" Then
            If isEmpty(a) Then
                a = token
            Else
                a = a & ";" & token
            End If
        End If
    Next

    ' return array
    strToArray = split(a, ";")
End Function
Function arrayToStr(a)
    Dim s
    Dim token

    For Each token in a
        If isEmpty(s) Then
            s = token
        Else
            s = s & ";" & token
        End If
    Next

    ' return string
    arrayToStr = s
End Function