none
Add new Computer Name to a Domain without Rebooting? Possible?

    Question

  • Forgive my script code. I'm a total noob, and my last "script" was in dos 6.2 making bootup disks and menus for my PC dos games.

    Okay, I got a script to rename a PC with model number + serial number (ie. Optiplex 380 w/ serial number xxxxx = "380-xxxxx")
    Currently, I have a script to rename the PC, then it adds a registry entry in the RunOnce to run a second script to add the computer to a Domain, it then restarts the PC. After reboots, the RunOnce initiates, and adds the computer, and reboots again.

    I'd like to know if it's possible to add the computer to the domain with its NEW name before the 1st reboot, and without the registry hack.


    Script to rename PC:

    Set-ExecutionPolicy unrestricted -force
    $oldComputerName = $env:COMPUTERNAME
    $ModelName = get-WmiObject Win32_ComputerSystem | % { $_.Model }
    $SplitName = $ModelName.Split(" ")
    $ModelNumber = $SplitName[1]
    gwmi Win32_SystemEnclosure | % { $NewComputerName = $ModelNumber + '-' + $_.SerialNumber }
    $computer = gwmi Win32_ComputerSystem -computername $oldComputerName
    $computer.Rename($NewcomputerName)
    
    Set-itemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce -name "JoinDomain" -value "e:\AddPC.lnk"
    restart-computer
    Second Script to Add Computer to Domain:
    $pwd = convertTo-secureString -asPlainText -force -string joindomain
    $userPwd = new-Object Management.Automation.PScredential `SHDOMAIN\joindomain,$pwd
    Add-Computer -DomainName SHDOMAIN -credential $userPwd
    restart-computer
    Tuesday, March 09, 2010 6:14 PM

Answers

  • Have you tried doing it?  I think it will work if you just do it in one step.  I'm fairly certain the wmi name change will be the one used when calling add-computer even though it hasn't officially changed yet:

    $oldComputerName = $env:COMPUTERNAME
    $ModelName = get-WmiObject Win32_ComputerSystem | % { $_.Model }
    $SplitName = $ModelName.Split(" ")
    $ModelNumber = $SplitName[1]
    gwmi Win32_SystemEnclosure | % { $NewComputerName = $ModelNumber + '-' + $_.SerialNumber }
    $computer = gwmi Win32_ComputerSystem -computername $oldComputerName
    $computer.Rename($NewcomputerName)
    $pwd = convertTo-secureString -asPlainText -force -string joindomain
    $userPwd = new-Object Management.Automation.PScredential `SHDOMAIN\joindomain,$pwd
    Add-Computer -DomainName SHDOMAIN -credential $userPwd
    I don't have a system to test it on right now.
    Wednesday, March 10, 2010 5:01 PM