Author Posts

August 27, 2015 at 8:24 am

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.

Thanks

starchaser

August 27, 2015 at 8:29 am

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

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

To be more specific this is the command that I am calling from a powershell script

ec2-create-tags

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

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

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

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

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

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.