Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating Windows local user account

Status
Not open for further replies.

martincarnegie

Technical User
Feb 28, 2006
31
CA
Hi all,

I have a piece of code that is being used to add an account to Windows 2003. The code works fine, but if a password is used that does not meet the password requirements, it reports success when it actually failed. I have tried to build some error checking into the code which is working but is doing something weird.

Here is the code

1 Set colAccounts = GetObject("WinNT://" & strComputer & "")
2 Set objNewUser = colAccounts.Create("user", strUserName)
3 WScript.Echo objNewUser
4 objNewUser.FullName = strFullName
5 objNewUser.Description = strDescription
6 objNewUser.SetPassword strPassword
7 objNewUser.SetInfo
8
9 if Err.Number = &H8007007B Then
10 WScript.Echo "Account not created"
11 WScript.Quit Err.Number
12 End If

So when this runs it will work fine. I added line 3 to just see what it was doing. If I comment out or remove that line the code does not work anymore. It always reports as exiting with &H8007007B. Any ideas?

Thanks

Martin

Martin Carnegie
GulfBreeze Software - GulfBreeze Blog -
 
.SetPassword should return an error code. Did you try echoing what it returns for both a good and a bad password?

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I added the line

WScript.Echo "Password: " & Hex(Err.Number)

after the password and commented out the line 3 again and it reports as 8007007B which is the same as after the setinfo. If I remove the comment, it comes back as D for the return.

I have no idea why that line is making a difference.

like the comment ;)

Martin Carnegie
GulfBreeze Software - GulfBreeze Blog -
 
Try changing this:

objNewUser.SetPassword strPassword

to this:

WScript.Echo objNewUser.SetPassword strPassword

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I get a message about a compilation error: expected end of statement.

The line and column point to the space between SetPassword and strPassword

WScript.Echo objNewUser.SetPassword(here)strPassword

I guess it does not like that spacing :)

Thanks for the help BTW.

Martin Carnegie
GulfBreeze Software - GulfBreeze Blog -
 
Ooops...my bad:


WScript.Echo objNewUser.SetPassword[red]([/red]strPassword[red])[/red]

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Check the docs for .SetPassword. It should be returning something. I can't remember what.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
[1] First, in the script, I don't see on error resume next directive, hence, the conditional block on err.number is totally pointless. You have to add that line at appropriate place. Where? It depends on what you want to check.

[2]>I added line 3 to just see what it was doing. If I comment out or remove that line the code does not work anymore.
Line 3
> wscript.echo objNewUser
would never work in any way. objNewUser is an object. Even if it is "nothing", the wscript.echo cannot handle it. It is runtime error of type mismatch.

[3] Coupling all the behaviour not exactly as you might have thought, the result and conclusion are not on a firm ground. So what is it that "the code is working but is doing something weird"?!!
 
1) I would have posted all the code but it is not mine so I was only posting the particular section that was failing. The "on error resume next" is declared at the beginning of the script

2) I actually did not think it would do anything, but I was just echoing out after every line

3) The weirdness is that if the line wscript.echo objNewUser is left uncommented, the code works perfectly. If the id is not created due to a bad password, then the Err is set and I can trap it. If I comment out that line, then the Err is never set.



Martin Carnegie
GulfBreeze Software - GulfBreeze Blog -
 
[4] In case the whole script works conditioning on inserting a completely wrong line of script, you can as well throw the whole language into the dust-bin without any regret.

[5] Expand this part.
[tt]
if Err.Number = &H8007007B Then
WScript.Echo "Account not created"
[red]'[/red]WScript.Quit Err.Number
End If
[blue]
colAccounts.filter=array("user")
for each obj in colAccounts
wscript.echo obj.name
next
wscript.quit[/blue] 'early exit in order not to interfer with what come next
[/tt]
In the case when you see "Account not created" echo, do you find the strUserName being echoed through the for loop?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top