Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 将传入的$args转换为字符串值_Powershell_Args - Fatal编程技术网

Powershell 将传入的$args转换为字符串值

Powershell 将传入的$args转换为字符串值,powershell,args,Powershell,Args,我的powershell脚本与一个外部程序接口,该程序以不同的格式发送不同数量的参数 primary site=Peachtree Street 并用逗号分隔 primary site=Peachtree Street, last logon=13 Nov 2013, sender-ip-10.10.10.10 我一直在互联网上搜索如何将$args转换为字符串,否则$args会删除逗号,我需要逗号 以下是我为$args所做的尝试 发送方ip=10.10.10.10,主站点位置=peachtr

我的powershell脚本与一个外部程序接口,该程序以不同的格式发送不同数量的参数

primary site=Peachtree Street
并用逗号分隔

primary site=Peachtree Street, last logon=13 Nov 2013, sender-ip-10.10.10.10
我一直在互联网上搜索如何将$args转换为字符串,否则$args会删除逗号,我需要逗号

以下是我为$args所做的尝试

发送方ip=10.10.10.10,主站点位置=peachtree street,创建人=jsmith

第一稿

write-host $args

$args = $args | Out-String

write-host $args
第一输出

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
第二稿

write-host $args

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

write-host $args
第二输出

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

write-host $args

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

write-host $args
第三输出

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
但是我如何保存逗号呢

请帮忙

像这样引用您的输入

"sender-ip=10.10.10.10, primary site location=peachtree street, created by=jsmith" 
逗号用于创建数组,但如果在文本周围加引号,则会将所有内容保存为单个字符串

我个人还建议您避免使用
args
,而是创建一个参数,这样您就可以像
-parametername“sender ip=10.10.10.10,primary site location=peachtree street,created by=jsmith”
那样调用它。或者至少使用
$args[0]
。如果有人忘记引号,事情可能会破裂,因为
$args
将变成一个数组

更新:这应该是可行的。然而,这是一个“肮脏的修正”

像这样引用你的意见

"sender-ip=10.10.10.10, primary site location=peachtree street, created by=jsmith" 
逗号用于创建数组,但如果在文本周围加引号,则会将所有内容保存为单个字符串

我个人还建议您避免使用
args
,而是创建一个参数,这样您就可以像
-parametername“sender ip=10.10.10.10,primary site location=peachtree street,created by=jsmith”
那样调用它。或者至少使用
$args[0]
。如果有人忘记引号,事情可能会破裂,因为
$args
将变成一个数组

更新:这应该是可行的。然而,这是一个“肮脏的修正”


您是否考虑过编写脚本,将其作为管道输入而不是参数

$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

那么您就不需要进行参数解析。

您是否考虑过编写脚本,将其作为管道输入而不是参数

$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

这样就不需要对参数进行解析。

因为您无法控制输入,类似的操作可能会使您返回到逗号分隔的列表

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

由于您无法控制输入,类似的操作可能会使您返回到逗号分隔的列表

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

人们似乎总是忘记了可怜的旧输出字段分隔符变量
$OFS
,例如:

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

当数组串接在一起显示为字符串时,数组元素之间将使用
$OFS
包含的任何字符串-通常是在双引号字符串中引用包含数组的变量时。

人们似乎总是忘记了旧的输出字段分隔符变量
$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

当数组连接在一起显示为字符串时,
$OFS
包含的任何字符串都将在数组元素之间使用-通常是在双引号字符串中引用包含数组的变量时。

但是如何引用输入?这就是我正在努力解决的问题。一个外部程序访问脚本并发送不同数量的参数,程序只是去掉逗号。如果我控制输入,那么问题就解决了!程序如何访问脚本?您可以控制它如何调用PS脚本吗?但是我如何引用我的输入?这就是我正在努力解决的问题。一个外部程序访问脚本并发送不同数量的参数,程序只是去掉逗号。如果我控制输入,那么问题就解决了!程序如何访问脚本?你能控制它如何调用PS脚本吗?我从没想过!让我调查一下,我会随时通知你的。非常感谢。它需要在示例脚本中使用引号来创建字符串。来自应用程序,它将已经是一个字符串。它只是没有被解析成一个基于字符串中空格的参数数组。我从来没有想过!让我调查一下,我会随时通知你的。非常感谢。它需要在示例脚本中使用引号来创建字符串。来自应用程序,它将已经是一个字符串。它只是没有被解析为基于字符串中空格的参数数组。