Solved

VB script to join computer to workgroup

Posted on 2013-11-11
1
1,110 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

839 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