This topic contains 8 replies, has 5 voices, and was last updated by
August 27, 2015 at 8:24 am #29056
I am trying to read-host text with spaces and save it into a variable in powershell.
However when the variable is passed as an argument to another command then it ignore the space.
$Name = read-host "enter text here"
enter text here: hello world
ec2-create-tags %1 -t "Name=%2" -t "Server=%3"
In the above command it puts Name= Hello and Server= World
I would like to store Name= Hello World
I am not sure if there is any formatting error? I tried the backtick " backtick " option but it didn't work either.
Any help would be appreciated.
August 27, 2015 at 8:29 am #29058KeymasterPoints: 1,704Rank: Community Hero
Why don't you just use "Name=$Name" if that's what you want? I guess I'm confused why you're using the % tokens.
August 27, 2015 at 8:59 am #29059
Thanks for the response Dan. When I use "Name=$Name" then I get the return value also as Name=$Name
I am using %tokens because the ec2-create-tags command is called from a batch file by another powershell script that reads the user inputs with read-host variables.
August 27, 2015 at 9:10 am #29060
To be more specific this is the command that I am calling from a powershell script
And I use tokens as variables from my powershell script.
"c:\bin\nameInstanceTagVal.cmd $instanceID $instanceName $Owner $Purpose"
this is the command that I am running from the batch file
ec2-create-tags %1 -t "Name=%2" -t "Owner=%6" -t "Purpose=%7"
Do you have any other ideas?
August 27, 2015 at 9:12 am #29061ParticipantPoints: 0Rank: Member
Under "Any help would be appreciated" ... get batch files completely out of the mix. You are creating unnecessary complications. Unless there is a TRULY compelling reason, it should be all PowerShell.
August 27, 2015 at 9:19 am #29062
Thanks for the response Bob. The reason for using the batch file is because the batch file is also setting up the environment variables for java.
ec2-create-tags is a command that is run via SOAP call using java and my powershell script is calling other batch files which uses the SOAP calls.
August 27, 2015 at 3:04 pm #29082ParticipantPoints: 0Rank: Member
You can also set environment variables in Powershell and just do everything in Powershell which would reduce the complexity a lot.
August 31, 2015 at 2:50 am #29147ParticipantPoints: 0Rank: Member
if you use powershell only for read-host...
... not use powershell at all and use "set /p cmd" command
August 31, 2015 at 5:58 am #29150
Thanks for the response Megamorf. I did exactly that by moving the code from batch files into the powershell script to eliminate the complexity. Also, I used the syntax in powershell as described in the command's help that I was trying to execute.
If you're using the command line tools on a Windows system, you might need to use quotation marks (for example, "key=value"). If you're using Windows Powershell, you might need to use a second set of quotation marks, escaped with backticks (for example, "`"key=value`"").
Thanks for all your help.
The topic ‘read-host to variable with spaces not working’ is closed to new replies.