Torgeir
Mon Oct 04 13:15:01 CDT 2004
Matthew Clark wrote:
> I am looking for a way to check to see if an account exists in XP, and
> if not, create it, and keep running the VBS code until it is created and
> has Admin privileges(just in case the user add or group add fails). The
> only script I have now is the following to create the user. The
> questions I have are:
>
> 1) Am I creating the user correctly using VBS? (The code does work, but
> is there an easier way?)
> 2) How can I accomplish the goal as stated above using some type of VBS
> code?
>
> Thank you,
>
> Matthew
>
> CODE
> ----
> WshShell.Run "NET USER AUTOCAD XXXXXXX /ADD /ACTIVE:YES /COMMENT:AUTOCAD
> /EXPIRES:12/31/2004 /FULLNAME:AUTOCAD /PASSWORDCHG:NO"
> WshShell.Run "NET LOCALGROUP ADMINISTRATORS AUTOCAD /ADD"
Hi
As far as I see it, there is no point in looping, trying again if
it fails. If the script fails on the first pass, it will fail on
subsequent passes as well.
Here is a script suggestion with error handling and "error" messages
for failing user creation (or failing group addition if user account
exist):
'--------------------8<----------------------
Set oWshNet = CreateObject("WScript.Network")
sComputer = oWshNet.ComputerName
sUserName = "AUTOCAD"
sGroupname = "Administrators"
On Error Resume Next
Set oUser = GetObject("WinNT://" & sComputer & "/" & sUserName & ",user")
If Err.Number <> 0 Then
' User account does not exist, create it.
oShell.Run "NET.EXE USER " & sUserName & " XXXXXXX /ADD" _
& " /ACTIVE:YES /COMMENT:AUTOCAD /EXPIRES:2004-12-31" _
& " /FULLNAME:AUTOCAD /PASSWORDCHG:NO", 0, True
End If
On Error Resume Next ' Try again
Set oUser = GetObject("WinNT://" & sComputer & "/" & sUserName & ",user")
If Err.Number = 0 Then
' Connect to the group
Set oGroup = GetObject("WinNT://" & sComputer & "/" & sGroupname)
' Add the user account to the group
' Use error handling in case it is a member already
On Error Resume Next
oGroup.Add(oUser.ADsPath)
' Error -2147023518 is "The specified account name is already
' a member of the local group."
If Err.Number <> 0 And Err.Number <> -2147023518 Then
MsgBox "Was not able to add user to group", _
vbExclamation + vbSystemModal, "User creation"
End If
Else
MsgBox "Was not able to create user", _
vbExclamation + vbSystemModal, "User creation"
End If
'--------------------8<----------------------
--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx