![]() This will create a subkey in the RunOnce registry key (assuming you are running Vista/7/2008) named "joinDomain" with the value of "C:\scripts\joinDomain.ps1" Name joinDomain -Value "C:\scripts\joinDomain.ps1" If you want to do that, you'd use something like this at the end of your Rename-Computer function: Set-Location -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce' Be aware though if you are going to do this, that the script that will be writing to the HKLM hive must be run as an administrator (especially important if you have UAC turned on). If you really wanted to get tricky, you could add some code to your rename script that would set the RunOnce registry key to launch the domain join script upon reboot. You could use the RunOnce registry key (/en-us/library/aa376977(v=vs.85).aspx) to run your domain join script automatically upon reboot, but you're still going to have to reboot for both operations. This particularly becomes important when you are first renaming the computer, and then trying to join a domain, as the kerberos handshake can not be completed without the network stack responding to the correct computer name.Īnother reason is that several registry keys make use of the computer name, and those keys cannot be changed while they are loaded into memory (this is incidentally also why some programs require a reboot to complete installation or uninstallation). Which is the only time they read the computer name, so if you were to rename the computer without a restart, the network and application services would not respond to the new computer name. One being that on NT based computers (I believe this started with Windows 2000), the Application and Network services read the computer name when they are started. There are actually several reasons that you have to reboot after renaming a computer, or when joining a domain (which is basically the same operation with validation by AD). Works but I want to eliminate the restart at step 2. ![]() Result: Everything works as expected, but extra restart required. I attempted to use credentials with the Get-WmiObject call in Rename-Computer, but it threw an error about not being able to use different credentials on the local system. I assume this is because domain credentials are required for the rename once it's joined to the domain. Result: Return value from Rename-Computer is 1326 (Logon failure: unknown user name or bad password). Result: Output from Rename-Computer indicates that name was changed, but after restart, name did not change, but computer was joined to domain Attempt 2 Write-Error "Exception occurred in Join-ComputerToDomain " $Error $_domainCredential = $Host.UI.PromptForCredential("Enter domain credentials", "Enter domain credentials to be used when joining computer to the domain", "", "NetBiosUserName")Īdd-Computer -DomainName $_domain -cred $_domainCredential Write-Host "Exception occurred in Rename-Computer " $ErrorĪs you can see, this function is really just a wrapper for the built-in cmdlet Add-Computer that gathers the domain name and creates some credentials to use. Write-Host "Error - return value of " $result.ReturnValue Write-Error "You need administrative rights to execute this cmdlet" $computer = Get-WmiObject -Class Win32_ComputerSystem Rename ComputerĪccording to my Internet research, PowerShell 2.0 at one point before release had a built-in cmdlet called Rename-Computer, but it was removed for reasons unknown in CTP 3. These are the PowerShell functions I've created for each step. ![]() Goal: On a computer running Windows Server 2008 R2, use PowerShell 2.0 to:Ĭondition: Steps 1 and 2 must be performed together, i.e., without a computer restart between them Functions I'm Using ![]()
0 Comments
Leave a Reply. |