I’m doing some script in Powershell to automate a task. This script is going to get arguments, such as:
PS > myscript.ps1 par=a,1,2,0.1 par=b,3,4,0.1 par=c,5,6,0.1 bin=file.exe exeargs="fixed args for file.exe"
In short, file.exe is an executable which accept parameters (including a, b and c) and this ps1 script is going to execute file.exe passing args a, b and c within the specified range, varying ’em by the specified precision.
The question is, I first split each $arg in $args by the character “=”, and then I should split them by “,” to get the specified values.
The thing is, when I do:
foreach ($arg in $args)
{
$parts = ([string] $arg).split("=")
Write-Host $parts[1]
}
The output is
a 1 2 0.1
b 3 4 0.1
c 5 6 0.1
file.exe
fixed args for file.exe
I.e., it already substituted the “,” character with a whitespace, so my second split should be with white space, not with comma.
Any guess on why does it happen?
Thanks in advance!
First of all why are you writing it like a C program or something? You don’t have to pass arguments like that, use
$argsand split on=etc. when Powershell has a more powerful concept of parameters, whereby you can pass the named paramters and arguments rather than doing the parsing that you are doing. ( More on parameters here: http://technet.microsoft.com/en-us/library/dd315296.aspx)With that said, let me answer your question:
What you are doing is when you pass in arguments like:
par=a,1,2,0.1 par=b,3,4,0.1 par=c,5,6,0.1 bin=file.exe exeargs="fixed args for file.exe"you are passing in array of arrays. The first element is the array with elements:
Ok coming to the split:
When you do
[string] $a, you are converting the array into a string. By default this means an array with elements1,2,3will become1 2 3.So your first argument there
par=a,1,2,0.1, becomespar=a 1 2 0.1and the split on=meansparts[1]becomesa 1 2 0.1, which is what you see.So how can you retain the comma?
Just make an array to be converted into a string with
,inbetween than space, right?Here are some ways to do that:
Using
-joinoperator:now you will see the output with the commas that you want.
Another way, using
$ofsspecial variable:(more on $ofs here: http://blogs.msdn.com/b/powershell/archive/2006/07/15/what-is-ofs.aspx )
Disclaimer – All this explanation to make you understand what is happening. Please do not continue this and use paramters.