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"

Re: Create local account in XP by Torgeir

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

Re: Create local account in XP by Matthew

Matthew
Mon Oct 04 13:25:01 CDT 2004

The reason I asked about looping is that it seems like sometimes the
user gets created, sometimes not. Also sometimes they are an
Administrator, sometimes just a regular user.
Thank you VERY much for the code. I'll see how it works (Of course it
will be better than what I had done.)

Matthew

Torgeir Bakken (MVP) wrote:
> 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<----------------------
>

Re: Create local account in XP by Matthew

Matthew
Tue Oct 05 09:06:58 CDT 2004

I tried the code you gave me, and the sComputer, sUsername, and
sGroupName are fine and are echoed fine. I do an echo of the oUser
variable, and it is blank, so I get the MsgBox that says Unable to
create user. Any idea?

Thanks,

Matthew

Matthew Clark wrote:
> The reason I asked about looping is that it seems like sometimes the
> user gets created, sometimes not. Also sometimes they are an
> Administrator, sometimes just a regular user.
> Thank you VERY much for the code. I'll see how it works (Of course
> it will be better than what I had done.)
>
> Matthew
>
> Torgeir Bakken (MVP) wrote:
>
>> 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<----------------------
>>

Re: Create local account in XP by Torgeir

Torgeir
Tue Oct 05 09:29:59 CDT 2004

Matthew Clark wrote:

> I tried the code you gave me, and the sComputer, sUsername, and
> sGroupName are fine and are echoed fine. I do an echo of the oUser
> variable, and it is blank, so I get the MsgBox that says Unable to
> create user. Any idea?
Hi

You can't "WScript.Echo" oUser, because it is an object that
"WScript.Echo" can't handle (if you had turned on internal error
checking again, you would have got the error "Microsoft VBScript
runtime error: Type mismatch" in that case).

Anyway, I see a major error in the script I posted, it is missing
the creation of the oShell object.

Add this line to the top of the script and see if it works better for you:


Set oShell = CreateObject("WScript.Shell")


--
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

Re: Create local account in XP by Matthew

Matthew
Tue Oct 05 09:58:07 CDT 2004

Torgeir,
I added the line, and it still does not work. Here is the updated code
that I am using. It is identical to what you gave me, but I did a
MsgBox to echo out some of the variables, and like you said, it is blank
when it returns the oUser variable. It ends up and returns the "Was not
able to create user."

Thanks,

Matthew

---------------
Set oShell = CreateObject("WScript.Shell")
Set oWshNet = CreateObject("WScript.Network")
sComputer = oWshNet.ComputerName
sUserName = "AUTOCAD"
sGroupName = "Administrators"
Dim oUser

On Error Resume Next
MsgBox sComputer & sUserName & sGroupName
Set oUser = GetObject("WinNT://" & sComputer & "/" & sUserName & ",user")
MsgBox oUser
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
------------------------------

Torgeir Bakken (MVP) wrote:
> Matthew Clark wrote:
>
>> I tried the code you gave me, and the sComputer, sUsername, and
>> sGroupName are fine and are echoed fine. I do an echo of the oUser
>> variable, and it is blank, so I get the MsgBox that says Unable to
>> create user. Any idea?
>
> Hi
>
> You can't "WScript.Echo" oUser, because it is an object that
> "WScript.Echo" can't handle (if you had turned on internal error
> checking again, you would have got the error "Microsoft VBScript
> runtime error: Type mismatch" in that case).
>
> Anyway, I see a major error in the script I posted, it is missing
> the creation of the oShell object.
>
> Add this line to the top of the script and see if it works better for you:
>
>
> Set oShell = CreateObject("WScript.Shell")
>
>

Re: Create local account in XP by Torgeir

Torgeir
Tue Oct 05 10:12:26 CDT 2004

Matthew Clark wrote:

> Torgeir,
> I added the line, and it still does not work. Here is the updated
> code that I am using. It is identical to what you gave me, but I did a
> MsgBox to echo out some of the variables, and like you said, it is blank
> when it returns the oUser variable. It ends up and returns the "Was not
> able to create user."
Hi

You might need to change the date format in the /EXPIRES parameter to
make "NET.EXE USER .." work, I had to do that.

This works for me, maybe not for you (your original was 12/31/2004):

/EXPIRES:2004-12-31



--
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

Re: Create local account in XP by Matthew

Matthew
Tue Oct 05 12:16:07 CDT 2004

That sure was a "DUH" Moment... So now it works, but it does not give
me an error message if the user exists, or if the user is already a
member of the Admin. Group. That isn't as big of a deal as just getting
it to create the user. Thanks for all your help, I really appreciate it.

Matthew

Torgeir Bakken (MVP) wrote:
> Matthew Clark wrote:
>
>> Torgeir,
>> I added the line, and it still does not work. Here is the updated
>> code that I am using. It is identical to what you gave me, but I did a
>> MsgBox to echo out some of the variables, and like you said, it is blank
>> when it returns the oUser variable. It ends up and returns the "Was not
>> able to create user."
>
> Hi
>
> You might need to change the date format in the /EXPIRES parameter to
> make "NET.EXE USER .." work, I had to do that.
>
> This works for me, maybe not for you (your original was 12/31/2004):
>
> /EXPIRES:2004-12-31
>
>
>