0

I'm trying to execute the code below and it is working:

PS C:\> Rename-Computer -NewName TEST -DomainCredential okadamanila\itservices

Given that hostname is given on the PS1 file - however, I would like to ask or prompt the user for input to substitute the hostname; I've tried using the code block below:

$hostname = read-host "Please enter your name"
Write-Host "YOUR PREFERRED HOSTNAME IS $hostname - INITIALIZING HOSTNAME CHANGE"
$credential = Get-Credential
PS C:\> Rename-Computer -NewName $name -DomainCredential $credential
Read-Host -Prompt "Press Enter to exit"

Where the input from the user would be the hostname that'll be used on the Rename-Computer function, it this possible?

EDIT:

The code block above seems to work, yet I'm encountering the issue below:

ERROR BEING ENCOUNTERED

  • 1
    What exactly is going wrong? If I do: $var = read-host "Question: " followed by $var I get inside $var what I entered at the question. – LPChip Sep 8 '17 at 10:49
  • If you have a dedicated function to get the username (Get-UserName would be the correct nomenclature, by the way), why would you put the Read-Host into a param()-block? I'd simply do while($true){[string]$name = Read-Host "Enter a user-name"; if($name.Length -gt 0){break}else{Write-Out "You have to enter a name!"}} – flolilo Sep 8 '17 at 11:06
  • If you want to use the param()-block so you can call the function and the name-parameter so it does not prompt, then I'd suggest you test the .Length of $name, as -name "" is a valid string that does not contain any name. So function Get-UserName(){param([string]$name); while($true){if($name.Length -eq 0){$name = Read-Host "enter a user-name"}else{Write-Out "You entered $name"; break}} Or is there an evident point I'm not seeing? – flolilo Sep 8 '17 at 11:18
0

Get rid of the prompt (PS C:>) before Rename-Computer. You still need to change the variable name (from myname to name) that you have in $myname = read-host "Please enter your name:" so it reads $name = read-host "Please enter your name:" and matches what you have as a variable name in your call to the function Rename-Computer -NewName $name -DomainCredential $credential).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.