我正在编写一个脚本来与第 3 方软件交互。
他们以以下格式发送字符串:
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")
当我用
执行代码时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
最佳答案
它将其解释为多个参数。您需要引用整个内容,以便它只知道一个参数:
&{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
关于string - Powershell - $args 去除逗号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20104165/