This VBScript program uses the ActiveX Directory Service Interface (ADSI) to create a new user in a Domain or on a Workstation. During the processing of the script the state will be shown in dialog boxes. If no user interaction is detected these dialog boxes will be closed automatically after 10 seconds. The script writes also a record into the log file C:\NewUserLog.txt, reporting either the success or the failure of this operation. This allows an unattended execution.
The purpose of this script is to demonstrate how to write a script which:
'************************************************ ' File: WSHADSICreateUser.vbs (WSH sample in VBScript) ' Author: Günter Born ' ' Uses ADSI to create a new user in Windows NT. ' Also creates/updates a log file allowing an ' unattended use of this script. ' ' In no way shall the author be liable for any ' losses or damages resulting from the use of this ' program. Use AS-IS at your own risk. ' ' The code is the property of the author. You may ' use the code and modify it, as far as this header ' remains intact. Further updates and other samples ' may be found on my site: ' http://www.borncity.de '************************************************ Option Explicit On Error Resume Next DIM WshShell, objDomain, objUser DIM name, fullname, descript, passw DIM server, title, datime datime = "[" & date & " " & time & "] " server = "//Wien" name = "Bill" fullname = "Bill Brown" descript = "My favorite user" passw = "Gateway" title = "WSH sample - by Günter Born" Set WshShell = WScript.CreateObject("WScript.Shell") WshShell.Popup "Create new user " & name, 5, title, vbOKonly Set objDomain = GetObject("WinNT:" & server) If err.number <> 0 Then AddLog (datime & "*** Error: " & err.number & _ " ADSI not supported on " & server) WshShell.Popup "*** Error: " & err.number & _ " ADSI not supported on " & server, 10, _ title, vbOKonly WScript.Quit ' Sorry, that's the end ... End if ' Now we try to create the user object and set the properties Set objUser = objDomain.Create("user", name) objUser.Description = descript objUser.FullName = fullname objUser.SetInfo ' Store settings If err.number = 0 Then ' Success? ' Yes, add record to logfile AddLog datime & "User " & user & " created on " & server ' Inform user with a short message (20 seconds) WshShell.Popup "User " & name & " created", 10, title, vbOKonly Else ' some problem ### If err.number = -2147022672 then ' User exists, Add error into logfile AddLog datime & "*** Error: User " & name & " already exists" ' Inform user with a short message (20 seconds) WshShell.Popup "*** Error: User " & name & " already exists", _ 10, title, vbOKonly Else ' Add error into logfile AddLog datime & "*** Error: " & err.number & _ " could not create user " & name ' Inform user with a short message (20 seconds) WshShell.Popup "*** Error: " & err.number & _ " could not create user " & name, 10, title, vbOKonly End if End if Set objUser = Nothing Set objDomain = Nothing '#### Helpers ##### Sub AddLog (txt) ' Add a record to the logfile Const file1 = "C:\NewUserLog.txt" ' Log file name Const ForAppending = 8 ' Append mode Dim fso, fi ' object variable ' Creates a FileSystemObject object Set fso = CreateObject("Scripting.FileSystemObject") ' open file, force create, if not exists Set fi = fso.OpenTextFile(file1, ForAppending, true) fi.WriteLine (txt) ' append log Set fi = Nothing End Sub '*** End |
(c) G. Born