简体   繁体   English

如何将带点的批处理参数传递给powershell脚本?

[英]How can I pass batch arguments with dots into a powershell script?

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. 然后我想将这些变量传递给powershell脚本。 The problem is, is that some of these variables look like: 问题是,这些变量中的一些看起来像:

-Dfoo.bar=baz -Dfoo.bar =巴兹

Powershell for some reason breaks it up into two args. 由于某种原因,Powershell将其分为两个方面。 On the command line, I can just add quotes around the arg and call it a day. 在命令行中,我可以在arg周围添加引号并将其称为一天。 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%" %*

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? 有人有什么想法吗?

If you call your script from CMD, it works just fine as-is: 如果你从CMD调用你的脚本,它可以正常工作:

C:\>.\foo.bat -Dfoo.bar=baz
args are -Dfoo.bar=baz

To fix the issue when you run the batch script from PowerShell use the stop parsing operator --% eg: 要从PowerShell运行批处理脚本时修复此问题,请使用stop parsing operator --% eg:

C:\ PS> .\foo.bat --% -Dfoo.bar=baz
args are -Dfoo.bar=baz

To pass things as literal you can enclose them in single quotes. 要将事物作为文字传递,您可以将它们用单引号括起来。 My suggestion would be to do that for your arguments, and then break them up once in PowerShell, and splat them to your command. 我的建议是为你的参数做这个,然后在PowerShell中将它们分解一次,并将它们发送到你的命令。

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

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

Then in Powershell: 然后在Powershell:

Param($PassedArgs)
$SplitArgs = $PassedArgs.trim("'").split(" ")
SomeCommand @SplitArgs

Something like this might work: 像这样的东西可能会起作用:

@echo off

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

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

Demonstration: 示范:

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

Single quotes around an argument prevent PowerShell from doing funky things with that argument. 参数周围的单引号阻止PowerShell使用该参数执行时髦的事情。 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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Windows批处理文件或linux shell脚本中传递10个以上的参数 - How can I pass more than 10 arguments in windows batch file or linux shell script 如何通过Powershell启动过程将带引号的参数传递给提升的批处理脚本 - How to pass quoted arguments to elevated batch script via powershell Start-Process 如何在PowerShell中将参数传递给嵌入式脚本? - How to pass arguments to an inline script in PowerShell? 如何将 arguments 传递给远程 powershell 脚本? - How to pass arguments to a remote powershell script? 我如何将参数从Windows批处理脚本传递给Java - How can i pass parametersfrom a windows batch script to java 如何将数据库名称从批处理传递到mysql脚本 - how can I pass database name from batch to mysql script 如何将参数或变量从一个 powershell 脚本传递到另一个? - How do I pass arguments or variables from one powershell script to another? 如何在不同的进程中运行PowerShell脚本并将参数传递给它? - How to run PowerShell script in a different process and pass arguments to it? 如何将命令行 arguments 传递给由 doskey 调用的 PowerShell 脚本 - How to pass command line arguments to a PowerShell script invoked by a doskey 如何将变量从批处理文件传递到Powershell脚本 - How to pass variable from Batch File to a Powershell Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM