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!"
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)
Please log in to answer
Posted by:
anonymous_9363
13 years ago
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, change
Set 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
13 years ago
Posted by:
Thegunner
13 years ago
Rating comments in this legacy AppDeploy message board thread won't reorder them,
so that the conversation will remain readable.
so that the conversation will remain readable.