/build/static/layout/Breadcrumb_cap_w.png

WMI script create user for Windows 7

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

Answers (3)

Posted by: anonymous_9363 12 years ago
Red Belt
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.
Posted by: jrobledo10 12 years ago
Yellow Belt
0
I want to push wireless setting to laptops what is the best script i can use?
Posted by: Thegunner 12 years ago
Second Degree Green Belt
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.
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ