Solved

VB script to join computer to workgroup

Posted on 2013-11-11
1
1,231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month8 days, 16 hours left to enroll

615 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