Glowie
Glowie

Reputation: 2309

Cast incoming $args as string value

My powershell script interfaces with an external program that sends varying number of arguments in the format

primary site=Peachtree Street

and is separated by commas

primary site=Peachtree Street, last logon=13 Nov 2013, sender-ip-10.10.10.10

I have been searching all over Internet how to cast $args as a string - otherwise $args removes the commas, and I need commas

Here is what I tried for $args

sender-ip=10.10.10.10, primary site location=peachtree street, created by=jsmith

1st Script

write-host $args

$args = $args | Out-String

write-host $args

1st Output

sender-ip=10.10.10.10 primary site location=peachtree street created by=jsmith
sender-ip=10.10.10.10
primary
site
location=peachtree
street
created
by=jsmith

2nd Script

write-host $args

$args = "'" + $args + "'"

write-host $args

2nd Output

sender-ip=10.10.10.10 primary site location=peachtree street created by=jsmith
'System.Object[] site location=peachtree System.Object[] by=jsmith'

3rd Script

write-host $args

foreach ($i in $args){
   $i = $i | Out-String
}

write-host $args

3rd Output

sender-ip=10.10.10.10 primary site location=peachtree street created by=jsmith
sender-ip=10.10.10.10 primary site location=peachtree street created by=jsmith

But how do I preserve the commas????

Please help!!!

Upvotes: 1

Views: 405

Answers (4)

Keith Hill
Keith Hill

Reputation: 201892

Folks always seem to forget about the poor old output field separator variable $OFS e.g.:

C:\PS> function foo {$OFS=',';"$args"}
C:\PS> foo sender-ip=10.10.10.10 primary site location=peachtree street created by=jsmith
sender-ip=10.10.10.10,primary,site,location=peachtree,street,created,by=jsmith

Whatever string $OFS contains will be used between the elements of an array when it concatenated together to display as a string - typically when a variable containing an array is referenced inside a double-quoted string.

Upvotes: 1

malexander
malexander

Reputation: 4700

Since you can't control your input, something like this may work to get you back to a comma separated list.

($args | select $_) -join ','

Upvotes: 1

Frode F.
Frode F.

Reputation: 54911

Quote your input like

"sender-ip=10.10.10.10, primary site location=peachtree street, created by=jsmith" 

Commas are used for creating arrays, but with quotes around the text it will save everything as a single string.

Personally I'd also suggest that you avoid args and create a parameter instead so you could call it like -paramtername "sender-ip=10.10.10.10, primary site location=peachtree street, created by=jsmith". Or at least use $args[0]. Things may break if someone forgets the quotes, since $args would then become an array.

UPDATE: This should work. However, it is a "dirty fix".

Write-Host (($args | % { $_ -join ", " }) -join " ")

Upvotes: 2

mjolinor
mjolinor

Reputation: 68331

Have you considered writig your script to take that as pipeline input instead of as arguments?

$script = @'
Begin{}
Process {$_}
End {}
'@

 $script | set-content testscript.ps1

 'primary site=Peachtree Street, last logon=13 Nov 2013, sender-ip-10.10.10.10' | ./testscript.ps1

primary site=Peachtree Street, last logon=13 Nov 2013, sender-ip-10.10.10.10

Then you're not subject to the argument parsing.

Upvotes: 1

Related Questions