+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 23
Scripts Thread, script to rename computer and join it to domain ... in Coding and Web Development; Im writing a batch file to make things a bit easier for me with new machines. I want it to ...
  1. #1

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,512
    Thank Post
    1,320
    Thanked 469 Times in 306 Posts
    Blog Entries
    6
    Rep Power
    199

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

    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?

  2. #2

    maniac's Avatar
    Join Date
    Feb 2007
    Location
    Kent
    Posts
    3,037
    Thank Post
    209
    Thanked 425 Times in 306 Posts
    Rep Power
    144
    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.

  3. #3
    actech's Avatar
    Join Date
    Mar 2008
    Location
    Australia
    Posts
    198
    Thank Post
    50
    Thanked 20 Times in 17 Posts
    Rep Power
    16
    This is a batch file that my predecessor left here. It comes in handy from time to time

    Code:
    :: @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.

  4. #4
    box_l's Avatar
    Join Date
    May 2007
    Location
    Herefordshire
    Posts
    424
    Thank Post
    67
    Thanked 89 Times in 74 Posts
    Rep Power
    61
    disable system restore

    Code:
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\default")
    
    Set objItem = objWMIService.Get("SystemRestore")
    errResults = objItem.Disable("")
    rename

    Code:
    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

    Code:
    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

  5. #5

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,512
    Thank Post
    1,320
    Thanked 469 Times in 306 Posts
    Blog Entries
    6
    Rep Power
    199
    Quote Originally Posted by box_l View Post

    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!

  6. #6

    Join Date
    Oct 2008
    Location
    Lincolnshire
    Posts
    2,154
    Thank Post
    12
    Thanked 224 Times in 214 Posts
    Rep Power
    66
    Group policy and SysPrep sounds like what is needed here

  7. #7
    box_l's Avatar
    Join Date
    May 2007
    Location
    Herefordshire
    Posts
    424
    Thank Post
    67
    Thanked 89 Times in 74 Posts
    Rep Power
    61
    something like

    Code:
    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

  8. #8

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,512
    Thank Post
    1,320
    Thanked 469 Times in 306 Posts
    Blog Entries
    6
    Rep Power
    199
    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

  9. #9

    RabbieBurns's Avatar
    Join Date
    Apr 2008
    Location
    Sydney
    Posts
    5,512
    Thank Post
    1,320
    Thanked 469 Times in 306 Posts
    Blog Entries
    6
    Rep Power
    199
    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.

  10. #10
    actech's Avatar
    Join Date
    Mar 2008
    Location
    Australia
    Posts
    198
    Thank Post
    50
    Thanked 20 Times in 17 Posts
    Rep Power
    16
    You could use wsname to change the workgroup first and then netdom to change the domain? Not sure if it would work but worth a try

  11. #11

    Join Date
    Dec 2009
    Posts
    603
    Thank Post
    8
    Thanked 36 Times in 34 Posts
    Rep Power
    17
    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=Impersonat e}!\\" & _
    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

  12. #12
    tommccann's Avatar
    Join Date
    Jun 2009
    Posts
    252
    Thank Post
    46
    Thanked 39 Times in 31 Posts
    Rep Power
    0
    Quote Originally Posted by 2097 View Post
    Script For changing PC name and joining to a Domain
    Doesnt work for me? am i missing something obvious here??


  13. #13
    TechSupp's Avatar
    Join Date
    Mar 2007
    Location
    South Yorkshire
    Posts
    1,875
    Thank Post
    295
    Thanked 120 Times in 101 Posts
    Rep Power
    40
    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 :-)

  14. #14
    Slewis's Avatar
    Join Date
    Jun 2007
    Location
    Bolton
    Posts
    48
    Thank Post
    2
    Thanked 11 Times in 9 Posts
    Rep Power
    16

    Lightbulb

    Quote Originally Posted by tommccann View Post
    Doesnt work for me? am i missing something obvious here??

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

  15. #15
    tommccann's Avatar
    Join Date
    Jun 2009
    Posts
    252
    Thank Post
    46
    Thanked 39 Times in 31 Posts
    Rep Power
    0
    Quote Originally Posted by Slewis View Post
    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

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. [Fog] Rename w/ serial # and join domain
    By Onslow in forum O/S Deployment
    Replies: 15
    Last Post: 20th July 2010, 09:42 AM
  2. Rename My Computer Via Registry
    By FN-GM in forum Windows 7
    Replies: 10
    Last Post: 14th September 2009, 08:43 PM
  3. No Domain Join during Setup
    By Nick_Parker in forum Windows 7
    Replies: 19
    Last Post: 24th August 2009, 01:13 PM
  4. Replies: 4
    Last Post: 6th August 2009, 10:25 AM
  5. Domain Rename = Anyone done it ?
    By pooley in forum Windows
    Replies: 3
    Last Post: 25th May 2006, 12:57 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •