Anyone good w/ VB Script? Need to remove computer from domain...

fly

omg
Oct 1, 2004
79,465
27,338
1,323
Marklar
₥86,480
Steam
mattressfish
I have a script here that will add a computer into a domain, but not if the computer account already exists in the domain. My limited VBS skills are preventing me from figuring it out.
 
Are you any good with VBScript?

Here is what I have to join a domain and create a computer account. However, none of this works if there already is a computer account. So I need to delete the account first, if it exists...

Code:
'******************************************************************************
' Name: JoinDomain
'
' Purpose: Join a system to a domain
'
' Inputs:  strComputer   Name of computer
'          strDomain     Name of domain which the system is to join
'          strOU         Name of OU where the computer account is to be placed
'          strUserName   Name of the user which is to be used for the join
'          strPassword   Password of the user used for the join
'
' Outputs: Sets blnRebootRequired to True if the join succeeds
'
' Requirements: Windows XP or newer OS
'
' Other: This procedure also sets the ActiveComputerName registry entry so that
'        the procedure uses the appropriate name during the join.
'
'******************************************************************************
Private Sub JoinDomain(strComputer, strDomain, strOU, strUserName, strPassword)

  On Error Resume Next

  objLogFile.WriteLine "-->Executing the JoinDomain procedure"

  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 objWMIService
  Dim ibjComputer
  Dim intError

  Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")

  For Each objComputer in objWMIService.InstancesOf("Win32_ComputerSystem")

    If UCase(objComputer.Domain) = UCase(strDomain) Then
        objLogFile.WriteLine vbTab & "No need to change domain membership"
    Else
      objLogFile.WriteLine vbTab & "Attempting to update the ActiveComputerName"

      Set objShell = CreateObject("Wscript.Shell")
      Err.Clear
      objShell.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\" & _
                        "ComputerName\ActiveComputerName\ComputerName", _
                        strComputer, "REG_SZ"
      If Err.Number = 0 Then
        objLogFile.WriteLine vbTab & "Successfully updated the ActiveComputerName"
      Else
        objLogFile.WriteLine vbTab & "Error updating the ActiveComputerName"
        objLogFile.WriteLine vbTab & Err.Number & " " & Err.Description
        blnFatalError = True
      End If
      Set objShell = Nothing

      ' If strOU is blank then set it to NULL
      If strOU = "" Then strOU = NULL

      objLogFile.WriteLine vbTab & "Joining system to the " & strDomain & " domain"

      intError = objComputer.JoinDomainOrWorkGroup(strDomain, _
                                                   strPassword, _
                                                   strDomain & "\" & strUserName, _
                                                   strOU, _
                                                   JOIN_DOMAIN + ACCT_CREATE)
      objLogFile.WriteLine vbTab & GetWMIError(intError)
  
      ' Even though the WMI error message usually states that no reboot is required.
      ' One is definitely required to update.
      If intError <= 1 Then
        blnRebootRequired = True
      Else
        blnFatalError = True
      End If
    End If

  Next

  Set objWMIService = Nothing

End Sub
 
I know I'm late to the party, but....

I don't think the problem is with the script. I think the problem is with the the account running the script. It probably doesn't have right to delete machine accounts, which it needs to re-add a machine to the domain. If the account has account operator rights, at least, you should be okay.
 
itburnswhenipee said:
I know I'm late to the party, but....

I don't think the problem is with the script. I think the problem is with the the account running the script. It probably doesn't have right to delete machine accounts, which it needs to re-add a machine to the domain. If the account has account operator rights, at least, you should be okay.

This man sounds like he knows what he's talking about, so i shall agree with him despite having no knowledge of the subject whatsoever.