Using WSH (VB) to add a domain user to local administrator's group under Windows XP/2003

Posted on 2006-04-26
Last Modified: 2008-02-01

I'm working on a script that I want to be able to run on my admin station and do the following:

- Query user for a domain user account name (service account)
- For each workstation(XP) on a list, add that domain user to the local administrator's group

I've tried:
strComputer = [computer name variable]
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")

Set objUser = GetObject("WinNT://" & strComputer & "/" & [user name variable] & ",user")

I also tried with:
Set objUser = GetObject("WinNT://" & strComputer & "/DOMAIN\" & [user name variable] & ",user")

I get a "Exception: Unknown Exception" error every time... I know that this is probably because I'm trying to change a domain account instead of a local account, but I can't find any info on how I would do this for a domain account.  Any suggestions?

And yes, the user executing the script has full administrative privilages  on the target machines.

And yes, I'm kind of new to programming this kind of script, why do you ask?

Question by:The_Dark_Rabbit
    LVL 14

    Expert Comment

    by:Shiju Sasidharan

    Here is a fine source code
    hope this will help you


    Author Comment

    Thanks for the quick response.  However, the code you pointed me to seems a little inpenetrable to me (as I mentioned, I'm a beginner).  I'm not sure which bits are useful to me.  If someone could help me find the bit I need on that page so that I can find a way to integrate it into my script (which actually does a few other tasks as well), then your link might be useful.  Otherwise, I'm looking for a quick piece of code, not a whole script.


    LVL 70

    Accepted Solution


    If you're trying to add a Domain User to a Local Security Group then this is probably where it's breaking:

    Set objUser = GetObject("WinNT://" & strComputer & "/" & [user name variable] & ",user")

    The statement is telling your script to connect to a Local User Account on strComputer - which won't exist.

    Instead you would need to do:

    Set objUser = GetObject("WinNT://" & strDomain & "/" & strUserName & ", user")

    Where strDomain is your NetBIOS Domain Name. You can get that programatically as well:

    Set objNetwork = CreateObject("WScript.Network")
    strDomain = objNetwork.UserDomain


    Author Comment

    Thanks a lot, Chris!  That's exactly what I was looking for.  I feel kind of dumb for not figuring that one out, but I am grateful nonetheless.


    LVL 70

    Expert Comment

    by:Chris Dent

    Glad I could help :)


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now