Prompt for and rename computer

Aug 9, 2013 at 2:54 AM
I tried adding the following:
try {
    Disable-UAC
    Update-ExecutionPolicy Unrestricted
    $NewComputerName = Read-ComputerName
    if ($NewComputerName -ne $Null) Rename-Computer $NewComputerName
    if(Test-PendingReboot){Invoke-Reboot}
    Set-ExplorerOptions -showHidenFilesFoldersDrives -showFileExtensions
    Enable-RemoteDesktop
 <snip>
    Write-ChocolateySuccess 'Boxstarter.AppSvr08R2'

    #  Ensure IIS has the latest environment variable changes
    iisreset
} catch {
  Write-ChocolateyFailure 'Boxstarter.AppSvr08R2' $($_.Exception.Message)
  throw
}

function Read-ComputerName {
    $attemptsLeft=3
    while(--$attemptsLeft -ge 0 -and !$val) {
        try{
            $ComputerName=Read-Host "Computer name to assign"
            return $ComputerName
        }
        catch { }
    }
    write-BoxstarterMessage "Unable to obtain new computer name. Proceeding without renaming the computer"
    return $null
}

Function Rename-Computer ([string]$NewComputerName) {
$ComputerInfo = Get-WmiObject -Class Win32_ComputerSystem 
$OldComputerName = $ComputerInfo.Name.Trim()
Write-BoxstarterMessage "Old computer name" + $OldComputerName + "New computer name" + $NewComputerName
$ComputerInfo.rename($NewComputerName) | Out-Null
}
I didn't get a prompt nor did I see anything in the console or logs that I could find. Basic question is how can I prompt for input similar to how the auto login password is handled?

Thanks,
John
Coordinator
Aug 9, 2013 at 3:28 PM
Took me a bit to catch the problem here. There are 3 issues here but one in particular essentially prevents the entire script from running:
  1. The functions should be at the top.
  2. This line:
    Write-BoxstarterMessage "Old computer name" + $OldComputerName + "New computer name" + $NewComputerName
    should be:
    Write-BoxstarterMessage "Old computer name $OldComputerName New computer name $NewComputerName"
  3. This is the big one:
    if ($NewComputerName -ne $Null) Rename-Computer $NewComputerName
    should be:
    if ($NewComputerName -ne $Null) {Rename-Computer $NewComputerName}
    The missing braces was causing the remainder of the script not to run. For some reason and I'm not entirely sure why now, this simply stopped the package in its tracks without any logging or errors.
Aug 9, 2013 at 4:57 PM
Thank you Matt, I didn't expect you to debug my script. That's what I get for starting with a simple one in Notepad++ and adding more and more! When I opened it in ISE the good 'ol red "squiggly" showed #3 right away. Good news is I've got a clean rename and reboot functioning now. Bad news is it prompted me again on reboot for the computer name. Need to dig into the docs and code for the proper way to schedule steps and use the persistent variable.
Coordinator
Aug 9, 2013 at 7:08 PM
Great. Yeah. I was just curious myself why the input wasn't working.