| Platform SDK: Active Directory, ADSI, and Directory Services |
The following VBScript adds a domain user/group to a machine local group on a member server or a computer running Windows NT Workstation/Windows 2000 Professional:
' The following script adds a domain user/group to a machine local group on a member server or Windows NT Workstation/Windows 2000 Professional:
Dim IADsCont As IADsContainer
Dim ListIADsMembers As IADsMembers
Dim Member As IADs
Dim GroupIADsGroup As IADsGroup
sComputer = InputBox("This script adds a domain user/group to a local group on a member server or workstation." & vbCrLf & vbCrLf & "Specify the computer name of the group where you want to add the member:")
sGroup = InputBox("Specify the name of the group to which you want to add the member:")
If sComputer = "" Then
MsgBox "No computer name was specified. You must specify a computer name."
Exit Sub
End If
If sGroup = "" Then
MsgBox "No group name was specified. You must specify a group name."
Exit Sub
End If
'''''''''''''''''''''''''''''''''''''''
'Bind to the computer
'''''''''''''''''''''''''''''''''''''''
'
Set IADsCont = GetObject("WinNT://" & sComputer & ",computer")
Set GroupIADsGroup = IADsCont.GetObject("LocalGroup", sGroup)
If (Err.Number <> 0) Then
BailOnFailure Err.Number, "on GetObject method"
End If
strText = ""
cmember = 0
'''''''''''''''''''''''''''''''''''''''
'Get display group name and its members
'''''''''''''''''''''''''''''''''''''''
'Get the name
strText = strText & vbCrLf & "The group " & GroupIADsGroup.Name & " currently has the following members:"
'Get the members object
Set ListIADsMembers = GroupIADsGroup.members
If (Err.Number <> 0) Then
BailOnFailure Err.Number, "on IADsGroup::members method"
End If
'Enumerate the members of the group from the members object
For Each Member In ListIADsMembers
strText = strText & vbCrLf & " " & Member.Name & " (" & Member.Class & ")"
cmember = cmember + 1
Next
If cmember = 0 Then
strText = strText & vbCrLf & " " & "No members"
End If
Call show_groups(strText, sComputer)
sNewMember = InputBox("Specify the ADsPath of the domain user or group to add:")
If sNewMember = "" Then
MsgBox "No member was specified. You must specify a member to add."
Exit Sub
End If
'''''''''''''''''''''''''''''''''''''''
'Bind to the member you want to add
'''''''''''''''''''''''''''''''''''''''
Set NewMember = GetObject(sNewMember)
If (Err.Number <> 0) Then
BailOnFailure Err.Number, "Could not bind to user or group to add."
End If
'Call show_groups(member.ADsPath, sComputer)
GroupIADsGroup.Add (NewMember.ADsPath)
'group.Add("WinNT://timto1923dom/yyyy")
'group.Add("WinNT://redmond /domain users")
If (Err.Number <> 0) Then
BailOnFailure Err.Number, "Could not add member to group."
End If
'''''''''''''''''''''''''''''''''''''''
'Confirm success and display group name and its members
'''''''''''''''''''''''''''''''''''''''
strText = "Member was successfully added."
strText = strText & vbCrLf & "The group " & GroupIADsGroup.Name & " now has the following members:"
'Get the members object
Set ListIADsMembers = GroupIADsGroup.members
If (Err.Number <> 0) Then
BailOnFailure Err.Number, "on IADsGroup::members method"
End If
'Enumerate the members of the group from the members object
For Each Member In ListIADsMembers
strText = strText & vbCrLf & " " & Member.Name & " (" & Member.Class & ")"
cmember = cmember + 1
Next
If cmember = 0 Then
strText = strText & vbCrLf & " " & "No members"
End If
Call show_groups(strText, sComputer)
'''''''''''''''''''''''''''''''''''''''
'Display subroutines
'''''''''''''''''''''''''''''''''''''''
Sub show_groups(strText, strName)
MsgBox strText, vbInformation, "Groups on " & strName
End Sub
Sub BailOnFailure(ErrNum, ErrText) strText = "Error 0x" & Hex(ErrNum) & " " & ErrText
MsgBox strText, vbInformation, "ADSI Error"
WScript.Quit
End Sub