将带引号的参数传递到PowerShell脚本 [英] Passing quoted arguments to PowerShell scripts
问题描述
脚本是为CLI编写的,在Microsoft世界中,引用文件的脚本参数可能经常带有空格,要求将其加引号.
Scripting is made for CLI and, in the Microsoft world, script arguments referring to files may often have spaces requiring them to be quoted.
这对我来说是个问题.考虑以下脚本:
This turns out to be problematic for me. Consider this script:
#testarg.ps1
for($i=0; $i -lt $args.length; $i++)
{
write-host "Arg$i = $($args[$i])"
}
如果我在PowerShell交互式环境中运行它,如下所示:
If I run it inside the PowerShell interactive environment, like this:
PS C:\script> .\testarg.ps1 "first arg" "second arg"
我得到了预期的结果,
Arg0 = first arg
Arg1 = second arg
但是,在交互模式之外,脚本的常规使用将是批处理.据我了解, Microsoft建议的方法从cmd.exe
运行脚本似乎是powershell.exe path\to\script.ps1
(路径中没有空格).但是:
But the ordinary use of a script would be, outside the interactive mode, in a batch one. As far as I understand, the Microsoft suggested way to run scripts from cmd.exe
seems to be powershell.exe path\to\script.ps1
(without space in path). But:
powershell.exe .\testarg.ps1 "first arg" "second arg"
给出不一致的结果:
Arg0 = first
Arg1 = arg
Arg2 = second
Arg3 = arg
我注意到要获得预期的结果,可以使用单引号:
I noted that to obtain the intended result I can use single quotes:
powershell.exe .\testarg.ps1 'first arg' 'second arg'
但是,当脚本由自动生成并传递参数的GUI工具运行时,和/或出于引用使用的一致性的原因,并且/或者在存在更多CLI工具的情况下,这通常是不可行的.
But this is often not viable when the script is to be run by GUI tools that automatically generate and pass the arguments, and/or in presence of more CLI tools for reason of consistency in the use of the quotes.
出于某些原因,使用-file
选项时:
For some reason when using the -file
option:
powershell.exe –file .\testarg.ps1 "first arg" "second arg"
::(reference to relative path '.\' is optional)
我再次得到预期的结果:
I get again the intended result:
Arg0 = first arg
Arg1 = second arg
因此,使用-file
选项时,双引号可以正常工作.这表明可以将ps1
文件(使用FTYPE.exe
)的Windows文件类型关联重新映射为以下内容:
So with the -file
option the double quotes work properly. This suggests that one can remap the Windows file type association for ps1
files (with FTYPE.exe
) to something like:
FTYPE Microsoft.PowerShellScript.1=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -file "%1" %*
鉴于FTYPE关联,我可以通过简单地编写以下内容来获得预期的结果:
Given the FTYPE association, I can obtain the intended result by simply writing:
testarg.ps1 "first arg" "second arg"
后者对我来说很令人满意,但是由于我是PowerShell的新手,所以我很想知道:
The latter is quite satisfying for me, but since I am newbie with PowerShell, I wonder:
- 对于常规脚本,批处理形式
powershell.exe –file path\to\script.ps1
和交互式形式(在PowerShell中)PS > path\to\script.ps1
是否等效?在某些情况下,两种形式可能会产生不同的输出/效果吗? - 脚本内部是否有可能按原样读取命令行(不带双引号)的解决方案?像
*%
代表cmd.exe
.
- For a general script, are the batch form
powershell.exe –file path\to\script.ps1
and the interactive form (from within PowerShell)PS > path\to\script.ps1
equivalent? Are there some instances when the two forms may produce different output/effects? - Is there a possible solution inside the script to read the command line as is (without stripping double quotes)? Something like
*%
forcmd.exe
.
推荐答案
在您撰写时,我不明白一件事:
There is one thing I don't understand when you wrote:
testarg.ps1 "first arg" "second arg"
您要打给哪个批次?
在PowerShell.exe
中,应这样写:
cd "c:\Temp"
& .\testarg.ps1 "first arg" "second arg"
在cmd.exe
中应该这样写:
powershell –file .\testarg.ps1 "first arg" "second arg"
这篇关于将带引号的参数传递到PowerShell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!