Jump to content

script to rename computer and join it to domain ...


Recommended Posts

Posted

Im writing a batch file to make things a bit easier for me with new machines. I want it to prompt for and then change the Computer name, enable file and printer sharing in the Firewall, Turn off System restore, and then run a couple of other things. Id also like a script for joining it to the domain which I would run after the previous script. Im not sure if this can be done in batchfile or if it would need to call a .vbs

 

Does anyone have anything doing any of the previous that I could use part of please?

Posted

Look at the NETDOM program, included with the windows support tools, which allows you to add machines to the domain and rename machines from the command line - I'm sure it wouldn't be to hard to write this into a batch file using the set /p command to prompt for user input where necessary.

 

Mike.

Posted

This is a batch file that my predecessor left here. It comes in handy from time to time

 

:: @ECHO OFF
:: This batch file makes the last changes to the first stage of build.

:: Changes the Workstation name and Workgroup

IF EXIST C:\build\wsname.txt GOTO JOIN
echo > wsname.txt c:\build\wsname.txt

Copy "C:\Build\Start Build.lnk" "C:\Documents and Settings\All Users\Start Menu\Programs\Startup\Start build.lnk"

wsname.exe /WG:ACW /NOREBOOT
wsname.exe /noreboot

ECHO Windows Registry Editor Version 5.00 > C:\Build\Wks.reg
ECHO. >> C:\Build\Wks.reg
ECHO [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon] >> C:\Build\Wks.reg
ECHO "DefaultDomainName"="%COMPUTERNAME%" >> C:\Build\Wks.reg
C:\Windows\Regedit.exe /s/i C:\Build\wks.reg

echo phase1 > c:\build\phase1.txt

Shutdown -r -t 15 -f

EXIT

:JOIN
IF EXIST c:\build\phase2.txt GOTO GROUPS
echo phase2 > c:\build\phase2.txt

cd \build
netdom.exe join %computername% /Domain:ACW /UserD:********** /PasswordD:*********
echo phase2 > c:\build\phase2.txt

::Shutdown -r -t 15 -f

It changes the name, reboots and then changes the domain. It is old and a bit clunky but works.

Posted

disable system restore

 

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\default")

Set objItem = objWMIService.Get("SystemRestore")
errResults = objItem.Disable("")

 

rename

 

Name = "GOOD-D5201"

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputers = objWMIService.ExecQuery _
   ("Select * from Win32_ComputerSystem")

For Each objComputer in colComputers
   err = objComputer.Rename(name)
Next

 

 

join domain

 

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
Const strOU					  = "ou=oucomputers, ou=test, ou=ouprojects, dc=Domain, dc=Com"
strDomain   = "xxxxxxx.hereford.sch.uk"
strPassword = "xxxxxxxxx"
strUser     = "Administrator"
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, _
   strOU, _
   JOIN_DOMAIN + ACCT_CREATE ,True)

strComputer = "."
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

 

hope these help

 

BoX

Posted

 

hope these help

 

BoX

 

cheers.. the system restore one completely removes the tab though, i was just wanting to tick the box to turn it off but still leave it there..

 

Also, is there any way the rename one can prompt for user input of computername, and then rename it to that? (i dont know any vbs at all)

 

Cheers!

Posted

something like

 

Dim computername
computername=InputBox("Enter your computername:")

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputers = objWMIService.ExecQuery _
   ("Select * from Win32_ComputerSystem")

For Each objComputer in colComputers
   err = objComputer.Rename(computername)
Next

Posted

Ive got it working fully now, with a couple of vbs called from the batch file, also reads in the domain username and password so I dont have my domain admin creds stuck in a plaintext file.

 

Its working great now. I dont know why they dont use sysprep, there must be a reason.

 

Ill post up my full script if anyones interested

  • 3 weeks later...
Posted

Is there a way to get netdom to remove any existing domain joinage before joining?

 

Ive a bunch that were joined prior to the image being taken, and netdom doesnt work as it says their already part of a domain.

  • 1 month later...
Posted

Script For changing PC name and joining to a Domain

 

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 message

Dim IP

strDomain = "DOMAINNAME"

strUser = administrator

strPassword = DOMAINPASSWORD

 

Set objNetwork = CreateObject("WScript.Network")

strComputer = objNetwork.ComputerName

 

Set objWMIService = GetObject ("winmgmts:" & "!\\" & strComputer & "\root\cimv2")

Set colAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")

 

title = "Join computer to domain"

message = "Please enter computer name. Leave blank or press cancel to quit." & vbCrLf & vbCrLf & "Generated name: " & generatedName

newComputerName = InputBox(message, title)

 

 

If newComputerName = "" Then

Wscript.quit

End If

 

areYousure = MsgBox("Are you sure you want t0 add computer to domain with name:" & vbCrLf & vbCrLf & newComputerName, vbYesNo + vbQuestion,"Add computer to domain")

 

If areYouSure = "7" Then

MsgBox "Exiting script.",vbInformation

Wscript.quit

End If

 

Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonate}!\\" & _

strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _

strComputer & "'")

 

ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strDomain & "\" & strUser, "ou=OUNAME,DC=DOMAINNAME,dc=local", _

JOIN_DOMAIN + ACCT_CREATE)

 

If ReturnValue = 0 Then

MsgBox "Computer added to domain under old name without error. proceeding to change computer name. "

Else

MsgBox "Computer not added to domain successfully. Return value: " & ReturnValue

End If

 

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

 

Set colComputers = objWMIService.ExecQuery _

("Select * from Win32_ComputerSystem")

 

 

For Each objComputer in colComputers

MsgBox "About to rename computer to: " & newComputername

ErrCode = objComputer.Rename(newComputerName, strPassword, strUser)

If ErrCode = 0 Then

MsgBox "Computer renamed correctly."

Else

MsgBox "Eror changing computer name. Error code: " & ErrCode

End If

 

Next

  • 1 month later...
Posted
Sounds like something I've been looking for to help when PC's suddenly decide they are not part of the domain, a quick script to get the computer name, take off domain, add to workgroup, reboot. then auto add back to domain (or is a reboot actually necessary?), anyone help? Must be nearly whats been posted already but I'm not quite up to that level of scripting :-)
Posted
Doesnt work for me? am i missing something obvious here??

 

:troll:

 

If you're erroring on Line 38, simply change "Impersonat e" to "Impersonate". (Remove the extra space character).

Posted
If you're erroring on Line 38, simply change "Impersonat e" to "Impersonate". (Remove the extra space character).

 

 

Nope line 1!

it says Const is not recognised as a internal or external command

  • 2 months later...
Posted
something like

 

Dim computername
computername=InputBox("Enter your computername:")

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputers = objWMIService.ExecQuery _
   ("Select * from Win32_ComputerSystem")

For Each objComputer in colComputers
   err = objComputer.Rename(computername)
Next

 

What needs to be changed to get it to work on windows 7? I did test it and it works fine on xp.

Cheers,

Nick

Posted (edited)

It runs fine in windows 7, well, if you discount the fact that you have to run it with elevated privileges !

 

disable UAC and all is well. (I do not recommend this)

run from an administrator cmd and all is well.

 

If you run actually logged as the local administrator (disabled by default), it also does the job.

 

I found a bit of code to loop the script back into itself with a UAC popup

 

If WScript.Arguments.length =0 Then
 Set objShell = CreateObject("Shell.Application")
 'Pass a bogus argument with leading blank space, say [ uac]
 objShell.ShellExecute "wscript.exe", Chr(34) & _
 WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else

Name = InputBox("Enter your computername:")

Set objWMIService = GetObject("Winmgmts:root\cimv2")

' Call always gets only one Win32_ComputerSystem object.
For Each objComputer in _
   objWMIService.InstancesOf("Win32_ComputerSystem")

       Return = objComputer.rename(Name)
       If Return <> 0 Then
          WScript.Echo "Rename failed. Error = " & Err.Number
       Else
          WScript.Echo "Rename succeeded." & _
              " Reboot for new name to go into effect"
       End If

Next


End If

 

This works for me.

 

OOOps!

 

that was different code to the original :o

 

this is the version i should have posted..

 

If WScript.Arguments.length =0 Then
 Set objShell = CreateObject("Shell.Application")
 'Pass a bogus argument with leading blank space, say [ uac]
 objShell.ShellExecute "wscript.exe", Chr(34) & _
 WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else

Dim computername
computername=InputBox("Enter your computername:")

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colComputers = objWMIService.ExecQuery _
   ("Select * from Win32_ComputerSystem")

For Each objComputer in colComputers
   err = objComputer.Rename(computername)
Next

End if

 

Both work tho'.

 

Just different implementations of the same thing.

 

 

BoX

Edited by box_l
Posted

sorry to resurect an old thread, but I could really use this script. When renaming the domain, I get a return value of 87 and it fails. Any ideas??

 

Many thanks

Posted

Hi,

Thanks for that, the rename script works fine.

I cant get the join domain script to work though, on windows 7 and server 2008R2

Any ideas?

Cheers

Posted (edited)

If WScript.Arguments.length =0 Then
 Set objShell = CreateObject("Shell.Application")
 'Pass a bogus argument with leading blank space, say [ uac]
 objShell.ShellExecute "wscript.exe", Chr(34) & _
 WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else


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
Const strOU = "ou=oucomputers, ou=test, dc=boxtech, dc=Co, dc=uk"
strDomain   = "domain"
strPassword = "password"
strUser     = "Administrator"
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, _
   strOU, _
   JOIN_DOMAIN + ACCT_CREATE ,True)

strComputer = "."
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
end if

 

tested on my xen setup: 2008r2 and windows 7

Edited by box_l
  • 2 weeks later...
Posted

Hi,

I still can not get it working.

Attached below is the code i was using, was there anything i missed?

Regards,

Nick

If WScript.Arguments.length =0 Then
 Set objShell = CreateObject("Shell.Application")
 'Pass a bogus argument with leading blank space, say [ uac]
 objShell.ShellExecute "wscript.exe", Chr(34) & _
 WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
Else


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
Const strOU = "ou=computers, dc=ogps, dc=wan"
strDomain   = "ogps"
strPassword = "password"
strUser     = "administrator"
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, _
   strOU, _
   JOIN_DOMAIN + ACCT_CREATE ,True)

strComputer = "."
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
end if

  • 3 years later...
Posted

Hello

 

Do you have posted somewhere the vbs in order to rename and add a computer in AD ?

 

Thanks in advance !!:)

  • 3 years later...
Posted

Dear Team,

 

I am trying to run the following vb script on Windows-7 (64) bit operating system.

 

Code is,

 

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 message

Dim IP

strDomain = "blrdomain.com"

strUser = administrator

strPassword = admin@321

 

Set objNetwork = CreateObject("WScript.Network")

strComputer = objNetwork.ComputerName

 

Set objWMIService = GetObject ("winmgmts:" & "!" & strComputer & "\root\cimv2")

Set colAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")

 

title = "Join computer to domain"

message = "Please enter computer name. Leave blank or press cancel to quit." & vbCrLf & vbCrLf & "Generated name: " & generatedName

newComputerName = InputBox(message, title)

 

 

If newComputerName = "" Then

Wscript.quit

End If

 

areYousure = MsgBox("Are you sure you want to add computer to domain with name:" & vbCrLf & vbCrLf & newComputerName, vbYesNo + vbQuestion,"Add computer to domain")

 

If areYouSure = "7" Then

MsgBox "Exiting script.",vbInformation

Wscript.quit

End If

 

Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonat e}!" & _

strComputer & "\root\cimv2:Win32_ComputerSystem.Name='" & _

strComputer & "'")

 

ReturnValue = objComputer.JoinDomainOrWorkGroup(strDomain, strPassword, strDomain & "" & strUser, "ou=Computer,DC=blrdomain,dc=com", _

JOIN_DOMAIN + ACCT_CREATE)

 

If ReturnValue = 0 Then

MsgBox "Computer added to domain under old name without error. proceeding to change computer name. "

Else

MsgBox "Computer not added to domain successfully. Return value: " & ReturnValue

End If

 

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _

& "{impersonationLevel=impersonate}!" & strComputer & "\root\cimv2")

 

Set colComputers = objWMIService.ExecQuery _

("Select * from Win32_ComputerSystem")

 

 

For Each objComputer in colComputers

MsgBox "About to rename computer to: " & newComputername

ErrCode = objComputer.Rename(newComputerName, strPassword, strUser)

If ErrCode = 0 Then

MsgBox "Computer renamed correctly."

Else

MsgBox "Eror changing computer name. Error code: " & ErrCode

End If

 

Next

 

 

Whereas it fails with below error,

 

Line:38

Char:1

Error:0x80041021

Code: 80041021

Source: (null).

 

Could you please help me to resolve the same.

 

Thanks,

Mohan

Posted

Total guess: Set objComputer = GetObject("winmgmts:{impersonationLevel=Impersonat e}!" & _

 

There's a space in Impersonat e, should be Impersonate

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...