Platform SDK: Active Directory, ADSI, and Directory Services

Example Code for Creating Users on a Member Server or Windows 2000 Professional

The following Visual Basic code creates a user on a member server or Windows 2000 Professional:

'Example: Creating a user on a member server or workstation
 
Dim cont As IADsContainer
Dim oUser As IADsUser
Dim v As Variant

'''''''''''''''''''''''''''''''''''''''
'Parse the arguments
'''''''''''''''''''''''''''''''''''''''
On Error Resume Next
 
sComputer = InputBox("This script creates a user on a member server or workstation." & vbCrLf & vbCrLf & "Specify the computer name:")
sUser = InputBox("Specify the user name:")
 
If sComputer = "" Then
    Exit Sub
End If
If sUser = "" Then
    Exit Sub
End If
 
'''''''''''''''''''''''''''''''''''''''
'Bind to the computer
'''''''''''''''''''''''''''''''''''''''
Set cont = GetObject("WinNT://" & sComputer & ",computer")
If (Err.Number <> 0) Then
   BailOnFailure Err.Number, "on GetObject method"
End If
 
'''''''''''''''''''''''''''''''''''''''
'Create the user
'''''''''''''''''''''''''''''''''''''''
Set oUser = cont.Create("user", sUser)
If (Err.Number <> 0) Then
   BailOnFailure Err.Number, "on IADsContainer::Create method"
End If
 
'''''''''''''''''''''''''''''''''''''''
'Write the user to the computer's security database.
'''''''''''''''''''''''''''''''''''''''
oUser.SetInfo
If (Err.Number <> 0) Then
   BailOnFailure Err.Number, "on IADs::SetInfo method"
End If
'''''''''''''''''''''''''''''''''''''''
'Read the user that was just created
'and display its name and its properties.
'''''''''''''''''''''''''''''''''''''''
If (Err.Number <> 0) Then
  BailOnFailure Err.Number, "on SetInfo method"
End If
strText = "The user " & sUser & " was successfully added."
strText = strText & vbCrLf & "The user has the following properties:"
'Refresh the property cache
oUser.GetInfo

strText = strText & "Number of properties: " & Count
For cprop = 1 To Count
  Set v = oUser.Next()
  If IsNull(v) Then
    Exit For
  End If
  strText = strText & vbCrLf & cprop & ") " & v.Name & " (" & v.ADsType & ") "
Next
show_items strText, sComputer
'''''''''''''''''''''''''''''''''''''''
'Display subroutines
'''''''''''''''''''''''''''''''''''''''
Sub show_items(strText, strName)
    MsgBox strText, vbInformation, "Create user on " & strName
End Sub