簡體   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