Powershell 中的后台作业
我正在尝试在后台运行一项作业,该作业是带有参数的 .exe,并且目标有空格。例如:
$exec = "C:\Program Files\foo.exe"
我想使用参数运行它:
foo.exe /param1 /param2, etc.
我知道 Start-Job
会这样做,但我尝试了很多不同的组合,它要么因为空格而给我一个错误,要么因为的参数。有人可以帮我解决这里的语法吗?我需要假设 $exec 是可执行文件的路径,因为它是配置文件的一部分,并且稍后可能会更改。
I'm trying to run a job in a background which is a .exe with parameters and the destination has spaces. For example:
$exec = "C:\Program Files\foo.exe"
and I want to run this with parameters:
foo.exe /param1 /param2, etc.
I know that Start-Job
does this but I've tried tons of different combinations and it either gives me an error because of the white space or because of the parameters. Can someone help me out with the syntax here? I need to assume that $exec
is the path of the executable because it is part of a configuration file and could change later on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种方法是使用带有参数块的脚本块。
如果有一个参数中带有空格,例如文件/文件夹路径,则应将其引用以将其视为单个项目。参数是传递给脚本块的数组。
此示例使用脚本块,但您也可以通过使用
Start-Job
cmdlet 的-FilePath
参数(而不是-ScriptBlock 参数。
这是另一个参数带有空格的示例:
这是一个使用
$args
内置变量而不是param
块的示例。One way to do this is use a script block with a param block.
If there is a single argument with a space in it such as a file/folder path it should be quoted to treat it as a single item. The arguments are an array passed to the script block.
This example uses a script block but you can also use a PowerShell script using the
-FilePath
parameter of theStart-Job
cmdlet instead of the-ScriptBlock
parameter.Here is another example that has arguments with spaces:
Here is an example using the
$args
built-in variable instead of theparam
block.安迪的伎俩总体上很有效。如果您有参数集,或者想要将复杂的信息移至作业中,您也可以尝试此技术:
并且在作业中...
我通常使用这两种方法。
我在以下情况下使用 -ArgumentList / ScriptBlock 参数:
如果我需要复杂的参数,并且它们不需要在内存中传递(或其他方式)方便拥有它们稍后在磁盘上),我将使用哈希表方法。
希望这有帮助
Andy's trick generally works very well. If you have parameter sets, or otherwise want to move complex information into the job, you can also try this technique:
and in the job...
I use both approaches routinely.
I use -ArgumentList / ScriptBlock parameters when:
If I need complex arguments, and they don't need to be passed in memory (or its otherwise convenient to have them on disk later), I'll use the hashtable approach.
Hope this Helps