Link to home
Start Free TrialLog in
Avatar of cbielich
cbielichFlag for United States of America

asked on

Create Local User Account specifying domain with VBS

I have a vbs script that works in creating a local user account as an administrator on a target computer. The only thing is that it creates it as a local admin if that computer as the domain. How can I specify a different domain, or should I say the domain of AD which would be "GVCC"
' get target machine name and password 
' from command line arguments 
strServer = wscript.arguments(0)
strUser = wscript.arguments(1)
strPwd = wscript.arguments(2) 
 
' connect to target machine and  
' create new user 
Set oServer = GetObject ("WinNT://" & strServer) 
Set oUser = oServer.Create ("user", strUser)

oUser.SetPassword strPwd 
oUser.SetInfo 

' add new user to 'Administrators' group 
Set Group = GetObject("WinNT://" & strServer & "/administrators,group") 
Group.Add(oUser.ADsPath) 
Group.Setinfo 
 
' release objects 
Set oServer = nothing 
Set oUser = nothing 
Set group= nothing

Open in new window

Avatar of OklahomaDave
OklahomaDave

You are using the older WinNT ADSI provider, and as such it only references local machines. If you want to add an administrative account to your domain, you might want to explore using ADO to query your Active Directory server.
Avatar of cbielich

ASKER

I dont want to add an administrative account to my domain I want to add an domain users as an administrator of a local machine. Can ADO do this? Is there no option for this with the code I have now?
Hi cbielich,

My apologies for misunderstanding. What you'll need to do is create one isntance of your WinNT ADSI provider that can talk to your domain, then another that talks to your local machine's Administrators group. Then, get the user you want from the domain instance, and add it to the Administrator group on the local box.

You would first set your oUser object to your desired domain account:

oUser  = GetObject('WinNT://GVCC/BobSmith, User")

Open in new window


You've already bound to the local group you want, so then just add the user to that group - and you've already done that! So you're 90% of the way there!

i hope this has helped!

-David


Can you show me how I need to integrate that into my existing code please :)
In the line where you set oUser, replace it with the line I provided and replace the "BobSmith" name with the name of the user you want to pull from the domain....

The difference here is that you're not creating a user, you're simply binding to an existing user that you can add to the local machine's administrator's group.
So do I use "Set" before it?

I replaced the line with

Set oUser  = GetObject('WinNT://GVCC/strUser, User")

and I get this error

Microsoft VBScript compilation error: Syntax error
ASKER CERTIFIED SOLUTION
Avatar of OklahomaDave
OklahomaDave

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
One other itme - You definitely need the "Set" keyword in front of the assignment. Any time you set a variable to an object reference in VBScript you need to use the Set keyword. You don't have to do it when you're setting regular values like strings or numbers.

-David