Hi all,

I am writing a script using WMI to create a user, set the password and then move it in to the Users Group.
The script is below, I am using it in a sub routine as well.

Is there a way I can use the strComputer without putting in a hardcoded name. I did use "." but didnt work.
Could someone who knows WMI take a look and see what I can improve please.

dim strComputer, strUser, objWMI, objUser, objLocalUser, objLocalUsers, objComputer, objGroup

strComputer = "USER-PC"
Set objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")


'Rename Admin Account
Set colAccounts = objWMI.ExecQuery ("Select * From Win32_UserAccount Where LocalAccount = True And Name = 'Admin'")

For Each objAccount in colAccounts
objAccount.Rename "lcladmin"
Next
wscript.echo "Adminstrator account renamed!"


'Create New Local User Account

Set objComputer = GetObject("WinNT://" & strComputer & "")
Set objLocalUser = objComputer.Create("user", "Testuser")

WScript.Echo "Testuseruser created!"

objLocalUser.SetPassword "Summer99"
'WScript.Echo "Testuser user password set!"

objLocalUser.SetInfo

Set objGroup = GetObject("WinNT://" & strComputer & "/Users,group")
wscript.echo objGroup.ADspath
set objLocalUser = GetObject("WinNT://" & strComputer & "/" & "Testuser" & ",User")

wscript.echo objlocaluser.ADsPath
objGroup.Add "WinNT://" & strComputer & "/" & "Testuser"

objLocalUser.AccountDisabled = False

objLocalUser.SetInfo
WScript.Echo "Local user created!"
0 Comments   [ + ] Show Comments

Comments

Please log in to comment

Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.

Answers

0
Could someone who knows WMII asked my good friend George Osborne Ogle. He was able to come up with a number of code samples, the neatest of which is probably this.

Your code - like so much I see - has zero error-trapping. The Golden Rule of programming - and this qualifies as that - is to always, ALWAYS assume that nothing will work. You should then code accordingly. For example, changeSet objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")toSet objWMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
If Not IsObject(objWMI) Then
'// Display an error here, then exit
End If
and so on.
Answered 07/06/2011 by: VBScab
Red Belt

Please log in to comment
0
I want to push wireless setting to laptops what is the best script i can use?
Answered 07/09/2011 by: jrobledo10
Yellow Belt

Please log in to comment
0
Fixed it now and added another section to get the computers name. Cheers VBScab. Haha on the George Osborne Ogle but was on a time frame and the searching I was doing at the time wasnt bring up what I was after.
Answered 07/10/2011 by: Thegunner
Second Degree Green Belt

Please log in to comment
Answer this question or Comment on this question for clarity