2013-11-20 45 views
0

我正在編寫腳本來與第三方軟件進行交互。

他們在格式發送字符串:

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

然而,當腳本執行

Write-host $args 

這是輸出

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

如何保留原始輸入字符串用逗號?

編輯:

下面的代碼

$args = $args.ToString() 
write-host '$args is' $args 

foreach ($i in $args){ 
    $line_array+= $i.split(",") 
    write-host '$line_array is' $line_array 

} 

foreach ($j in $line_array){ 
    $multi_array += ,@($j.split("=")) 
} 

foreach ($k in $multi_array){ 
    $my_hash.add($k[0],$k[1]) 

} 


$Sender_IP = $my_hash.Get_Item("sender-ip") 

SNIPPIT當我與

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

執行代碼,我得到

$args is System.Object[] 
$line_array is System.Object[] 
$Sender_IP is 

回答

1

它的解釋是爲多個參數。您需要引用整個事情,所以它知道它只是一個參數:

&{Write-Host $args} sender-ip=10.10.10.10, primary site location=peachtree street, created by=jsmith 
&{Write-Host $args} 'sender 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 
sender 10.10.10.10, primary site location=peachtree street, created by=jsmith 
+0

要麼,他可以重新加入的論點是這樣的:'@ {寫主機(的$ args -join「」) } sender-ip = ...' –

+0

第三方程序正在發送讀取到$ args的參數。我想弄清楚把整個$ args作爲一個字符串。我嘗試了$ args = $ args.ToString(),但那不起作用..... – Glowie

+0

此外,第三方程序會傳遞不同數量的參數,我也必須處理它... – Glowie