Solved

VB script to join computer to workgroup

Posted on 2013-11-11
1
1,034 Views
Last Modified: 2013-11-11
According to MS I can join a computer to a workgroup using the code found here:
http://msdn.microsoft.com/en-us/library/aa392154(v=vs.85).aspx

It states about JOIN_DOMAIN: Default. Joins a computer to a domain. If this value is not specified, the join is a computer to a workgroup.

I want to disconnect the computer from the domain, without delete the account in AD, and join a workgroup named AHC. I will later reset the computer account in AD, shutdown this computer and join another computer using the same name.

Everytime I run my code below I get a return code of 2691 which means it is already joined. A restart of the computer show me it is still a member of the domain and NOT the workgroup I need.

Since I am logged in as a domain admin I should not need the user creds.

How do I get this to disconnect from the domain and become a member of the AHC workgroup?

MS code reads
Const JOIN_DOMAIN             = 1
Const ACCT_CREATE             = 2
Const ACCT_DELETE             = 4
Const WIN9X_UPGRADE           = 16
Const DOMAIN_JOIN_IF_JOINED   = 32
Const JOIN_UNSECURE           = 64
Const MACHINE_PASSWORD_PASSED = 128
Const DEFERRED_SPN_SET        = 256
Const INSTALL_INVOCATION      = 262144
strDomain   = "FABRIKAM"
strPassword = "ls4k5ywA"
strUser     = "shenalan"
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
Set objComputer = _
    GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
    strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" _
    & strComputer & "'")
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
    strPassword, _
    strDomain & "\" & strUser, _
    NULL, _
    JOIN_DOMAIN + ACCT_CREATE)

Open in new window


My mod reads:
Const JOIN_DOMAIN = 1
Const ACCT_CREATE = 2
Const ACCT_DELETE = 4
Const WIN9X_UPGRADE = 16
Const DOMAIN_JOIN_IF_JOINED = 32
Const JOIN_UNSECURE = 64
Const MACHINE_PASSWORD_PASSED = 128
Const DEFERRED_SPN_SET = 256
Const INSTALL_INVOCATION = 262144
 
strDomain = "AHC"
strPassword = NULL
strUser = NULL
 
Set objNetwork = CreateObject("WScript.Network")
strComputer = objNetwork.ComputerName
 
Set objComputer = _
	GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _
	strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _
	strComputer & "'")
 
ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, _
	strPassword, _
	strDomain & "\" & strUser, _
	NULL)
	
Wscript.Echo ReturnValue

Open in new window

0
Comment
Question by:pphreadr
1 Comment
 

Accepted Solution

by:
pphreadr earned 0 total points
ID: 39640144
I just figured this out. the computer must be UN-joined first, then it can join the workgroup.

Const NETSETUP_ACCT_DELETE = 2
UnJoinReturnValue = objComputer.UnjoinDomainOrWorkGroup(NETSETUP_ACCT_DELETE)

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

932 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

12 Experts available now in Live!

Get 1:1 Help Now