带双引号的参数没有通过 ArgumentList 正确传递给 Scriptblock [英] Parameters with double quotes are not properly passed to Scriptblock by ArgumentList

查看:38
本文介绍了带双引号的参数没有通过 ArgumentList 正确传递给 Scriptblock的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写通用的 powershell 脚本来在远程机器上执行部署.我遇到了一个不能超限的问题,这个问题是关于参数列表中的 Scriptblock 中带双引号的参数

I'm writing generic powershell script to perform deployments on remote machines. I have hit one problem I can not overrun, and this problem is with parameters with double quotes in Scriptblock passed by ArgumentList

我有这样的事情:

$remoteAddress = "some-pc"
$deploymentCommand = "D:\some path\Command.exe"
$deploymentPackages = @(`""package - one - external"`", `""package - two - external"`", `""package - three - internal"`")

foreach ($deploymentPackage in $deploymentPackages)
{
invoke-command -ComputerName $remoteAddress -ScriptBlock { param ($deployCmd, $deployPackage) &  $deployCmd -package:$deployPackage -action:doit } -ArgumentList   $deploymentCommand,$deploymentPackage
}

我在 $deploymentPackages 中添加了双引号.而且我仍然像这样远程执行我的命令(这当然失败了):

I have added double quotes in $deploymentPackages. And still I'm getting my command executed remotly like this (which of course fails):

D:\some path\Command.exe -package:package - one - external -action:doit
D:\some path\Command.exe -package:package - two - external -action:doit
D:\some path\Command.exe -package:package - three - external -action:doit

而正确的执行路径应该是:

while proper execution path should be:

D:\some path\Command.exe -package:"package - three - external" -action:doit

在包周围没有引号 - 一个 - 外部会搞砸一切

without quotes around package - one - external which mess up everythig

如何解决这个问题,因为我已经测试了许多解决方案,但没有一个有效.

How to overrun this problem, because i have tested number of solutions and non of them worked.

提前感谢您的帮助!

推荐答案

你可以这样做

$remoteAddress = "some-pc"
$deploymentCommand = "D:\some path\Command.exe"
$deploymentPackages = @('package - one - external', 'package - two - external', 'package - three - internal')
$remoteScript = {
    param( $deployCmd, $deployPackage )
    & $deployCmd "-package:$deployPackage" -action:doit
}

foreach ($deploymentPackage in $deploymentPackages)
{
   invoke-command -ComputerName $remoteAddress -ScriptBlock $remoteScript -ArgumentList $deploymentCommand,$deploymentPackage
}

这将 -package:<some string here> 在传递给您的可执行文件时绑定到单个参数中,这与执行类似 -package:"aaa bbb ccc" 的操作相同 在 cmd.exe 中.

This bundles -package:<some string here> into a single argument when passed to your executable, which is the same as doing something like -package:"aaa bbb ccc" in cmd.exe.

我假设您不希望将文字引号传递给 exe,只是为了 -package:<some string here> 作为单个参数而不管 <some 中的空格字符串在这里>

I assume you don't want literal quotes passed to the exe, just for -package:<some string here> to be a single argument regardless of spaces in <some string here>

如果您希望将文字引号传递给 exe,请使用上面的代码与

If you want literal quotes to be passed to the exe, use the above code with

& $deployCmd "-package:`"$deployPackage`"" -action:doit

这篇关于带双引号的参数没有通过 ArgumentList 正确传递给 Scriptblock的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆