繁体   English   中英

在 Powershell 中传递带空格的参数列表

[英]Passing an argument list with spaces in Powershell

我正在使用带有 arguments 的 Start-Process 来启动 powershell 中的程序。

$ServerToStart = "C:/a/b/c/xyz.exe"

Start-Process $ServerToStart -ArgumentList "a, b, Foo Bar"

Arguments a & b 工作完美,但是,参数“Foo Bar”的“Bar”被忽略,只接受“Foo”——大概是因为空间。

powershell怎么解决这个问题?

您是否尝试过将参数封装在引号中:

Start-Process $ServerToStart -ArgumentList "a, b, 'Foo Bar'"

根据 PS 文档,“如果参数或参数值包含空格,则需要用转义的双引号将它们括起来。有关更多信息,请参阅about_Quoting_Rules 。”

所以,在这种情况下,我认为正确的方法是

Start-Process $ServerToStart -ArgumentList "a, b, `"Foo Bar`""

Arguments 必须正确格式化才能执行。 这是 MS 提供的文档中一个非常有据可查的用例:

• PowerShell:运行可执行文件

https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

5. The Call Operator &


Why: 

Used to treat a string as a SINGLE command. Useful for dealing with spaces.

In PowerShell V2.0, 
if you are running 7z.exe (7-Zip.exe) or another command that starts with a number, you have to use the command invocation operator &.

The PowerShell V3.0 parser do it now smarter, in this case you don’t need the & anymore .

Details: 
Runs a command, script, or script block. The call operator, also known as the "invocation operator," lets you run commands that are stored in variables and represented by strings. Because the call operator does not parse the command, it cannot interpret command parameters

# Example:

& 'C:\Program Files\Windows Media Player\wmplayer.exe' "c:\videos\my home video.avi" /fullscreen

Things can get tricky when an external command has a lot of parameters or there are spaces in the arguments or paths!

With spaces, you have to nest Quotation marks and the result it's not always clear! 

In this case it is better to separate everything like so:

$CMD = 'SuperApp.exe'
$arg1 = 'filename1'
$arg2 = '-someswitch'
$arg3 = 'C:\documents and settings\user\desktop\some other file.txt'
$arg4 = '-yetanotherswitch'

& $CMD $arg1 $arg2 $arg3 $arg4

# or same like that:

$AllArgs = @('filename1', '-someswitch', 'C:\documents and settings\user\desktop\some other file.txt', '-yetanotherswitch')
& 'SuperApp.exe' $AllArgs

至于使用 Start-Process,来自同一篇文章:

7. Start-Process  (start/saps)


Why: Starts a process and returns the .Net process object Jump if -PassThru is provided. It also allows you to control the environment in which the process is started (user profile, output redirection etc). You can also use the Verb parameter (right-click on a file, that list of actions) so that you can, for example, play a wav file.

Details: Executes a program returning the process object of the application. 
Allows you to control the action on a file (verb mentioned above) and control the environment in which the app is run. You also have the ability to wait on the process to end. You can also subscribe to the processes Exited event.
#>

# Example:

#starts a process, waits for it to finish and then checks the exit code.
$p = Start-Process ping -ArgumentList "invalidhost" -wait -NoNewWindow -PassThru
$p.HasExited
$p.ExitCode

此外,如果您使用的是 Start-Process:

启动过程 | 微软文档

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-7.1#example-7--specifying-arguments-to-the-process

# Example 7: Specifying arguments to the process

Both commands start the Windows command interpreter, issuing a dir command on the Program Files folder. Because this foldername contains a space, the value needs to be surrounded with escaped quotes. Note that the first command specifies a string as ArgumentList. The second command is a string array.
#>

Start-Process -FilePath "$env:comspec" -ArgumentList "/c dir `"%systemdrive%\program files`""
Start-Process -FilePath "$env:comspec" -ArgumentList "/c","dir","`"%systemdrive%\program files`""

根据我的经验,它通常是这样工作的:

$ServerToStart = "C:/a/b/c/xyz.exe"

Start-Process $ServerToStart -ArgumentList "a", "b", "Foo Bar"

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM