如何传递用点批量参数到PowerShell脚本? [英] How can I pass batch arguments with dots into a powershell script?

查看:439
本文介绍了如何传递用点批量参数到PowerShell脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有将由外部进程,是不是我的控制下运行批处理脚本。外部进程可以提供的参数变量量批处理脚本。那么我想这些变量传递到PowerShell脚本。问题是,是,一些变量如下:

I have a batch script that will be run by an an external process that is not under my control. The external process can supply a variable amount of arguments to the batch script. I then want to pass these variables to a powershell script. The problem is, is that some of these variables look like:

-Dfoo.bar =巴兹

-Dfoo.bar=baz

Powershell的出于某种原因打破它分为两个ARGS。在命令行,我可以再补充引号的Arg和收工。但如何我会得到一批通过这种方式?这里是我的脚本:

Powershell for some reason breaks it up into two args. On the command line, I can just add quotes around the arg and call it a day. But how would I get batch to pass it that way? Here is my script:

@echo off
SET CMD=C:\Scripts\foo.ps1

PowerShell.Exe -Command "%CMD%" %*

我注意到这个问题是非常相似的<一个href=\"http://stackoverflow.com/questions/12223790/escaping-arguments-when-passing-from-batch-script-as-args-to-powershell-scri\">this 之一。在这里,他的转义$字符。我试着做了点和/或破折号字符类似的东西,但没有运气。任何人有任何想法?

I noticed this question is very similar to this one. Here he's escaping the $ character. I tried doing something similar for the dot and/or the dash char, but have no luck. Anyone has any ideas?

推荐答案

这样的事情可能工作:

@echo off

set "CMD=C:\Scripts\foo.ps1"
set "ARGS=%*"
set "ARGS=%ARGS: =' '%"

PowerShell.Exe -Command "%CMD%" '%ARGS%'

示范:

C:\>type test.cmd
@echo off

set "CMD=C:\test.ps1"
set "args=%*"
set "args=%args: =' '%"
powershell -Command "%CMD%" '%args%'

C:\>type test.ps1
$args | % { $_ }

C:\>test.cmd -Dfoo.bar=baz -something
-Dfoo.bar=baz
-something

从周围做时髦的事情这样的说法参数prevent PowerShell的单引号。通过在参数列表与替换空间''你把单引号一个参数后在下单前。周围的变量的外单引号前的第一个和最后一个参数后添加缺少的引号。

Single quotes around an argument prevent PowerShell from doing funky things with that argument. By replacing spaces in the argument list with ' ' you put single quotes after one argument and before the next one. The "outer" single quotes around the variable add the missing quotes before the first and after the last argument.

C:\>type test2.cmd
@echo off
set "args=%*"
echo %args%
set "args=%args: =' '%"
echo %args%
echo '%args%'

C:\>test2.cmd -Dfoo.bar=baz -something
-Dfoo.bar=baz -something
-Dfoo.bar=baz' '-something
'-Dfoo.bar=baz' '-something'

这篇关于如何传递用点批量参数到PowerShell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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