Re: Join Workgroup from command line

From: Andrew Mitchell (amitchel_at_removecasey.vic.gov.au)
Date: 03/18/04


Date: Thu, 18 Mar 2004 04:41:09 -0800


"=?Utf-8?B?TGVmdGVyaXM=?=" <anonymous@discussions.microsoft.com> said

> Hi,
> Is there a way to join a windows 2000 pro computer to a workgroup from
> command line or vbscript? Any help is greatly appreciated.
> Lefteris
>

Try this as a .vbs file. (watch for line wrapping)

Andy.

'Adds a computer to a Workgroup or Domain then reboots.

'Set the strDomain, strUser and strPassword to suit your environment

Option Explicit

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

Dim strDomain, strPassword, strUser, strComputer, ReturnValue
Dim objComputer, objNetwork, objWMIService, objOperatingSystem
Dim colOperatingSystems

'Domain or workgroup name
strDomain = "Domain_Or_Workgroup_Name"

'Account details with access to add computers to domain or workgroup
strUser = "Account"
strPassword = "Password"

'Add the computer to the domain/workgroup
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)

'Display Completion Message to user
Dim objShell, intValue

Set objShell = CreateObject("WScript.Shell")
intValue = objShell.Popup("Your computer has been added to the ' &
strDomain & ' Domain." _
                & vbCRLF & "Your computer will now reboot.", , , vbExclamation
+ vbOKOnly)

'Reboot PC
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate,(Shutdown)}!\\" & strComputer &
"\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
    ("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    ObjOperatingSystem.Reboot()
Next



Relevant Pages

  • Re: Mail Merge DDE
    ... You can avoid using DDE if the /only/ problem is that you are using a Workgroup /and/ you do not mind having the Workgroup security details stored in either a ..odc file or the Word document ... Dim strConnection As String ...
    (microsoft.public.word.mailmerge.fields)
  • Disable Inactive users
    ... Dim objRootDSE, strConfig, objConnection, objCommand, strQuery ... Dim strDN, dtmDate, objDate, lngDate, objList, strUser ... On Error GoTo 0 ...
    (microsoft.public.windows.server.scripting)
  • Re: Comparing Two Files
    ... Dim arrFolders, strFolders, strFolder ... Dim arrUsers, strUsers, strUser ...
    (microsoft.public.scripting.vbscript)
  • Re: Scrip Help
    ... dim objUser, objTrans, objFSO, objFolder ... strUser = InputBox ...
    (microsoft.public.windows.server.scripting)
  • Re: Administer users via code
    ... Dim strUser As String ... Dim strOldPW1 As String ... The first thing required is to create an admin workspace. ...
    (microsoft.public.access.security)