Powershell 中的后台作业

发布于 2024-12-24 17:17:31 字数 321 浏览 4 评论 0原文

我正在尝试在后台运行一项作业,该作业是带有参数的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听闻余生 2024-12-31 17:17:31

一种方法是使用带有参数块的脚本块

如果有一个参数中带有空格,例如文件/文件夹路径,则应将其引用以将其视为单个项目。参数是传递给脚本块的数组。

此示例使用脚本块,但您也可以通过使用 Start-Job cmdlet 的 -FilePath 参数(而不是 -ScriptBlock 参数。

这是另一个参数带有空格的示例:

$scriptBlock = {
    param (
        [string] $Source,
        [string] $Destination
    )
    $output = & xcopy $Source $Destination 2>&1
    return $output
}

$job = Start-Job -scriptblock $scriptBlock -ArgumentList 'C:\My Folder', 'C:\My Folder 2'
Wait-Job $job
Receive-Job $job

这是一个使用 $args 内置变量而不是 param 块的示例。

$scriptBlock = {
    $output = & xcopy $args 2>&1
    return $output
}

$path1 = "C:\My Folder"
$path2 = "C:\My Folder 2"

"hello world" | Out-File -FilePath  "$path1\file.txt"

$job = Start-Job -scriptblock $scriptBlock -ArgumentList $path1, $path2
Wait-Job $job
Receive-Job $job

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 the Start-Job cmdlet instead of the -ScriptBlock parameter.

Here is another example that has arguments with spaces:

$scriptBlock = {
    param (
        [string] $Source,
        [string] $Destination
    )
    $output = & xcopy $Source $Destination 2>&1
    return $output
}

$job = Start-Job -scriptblock $scriptBlock -ArgumentList 'C:\My Folder', 'C:\My Folder 2'
Wait-Job $job
Receive-Job $job

Here is an example using the $args built-in variable instead of the param block.

$scriptBlock = {
    $output = & xcopy $args 2>&1
    return $output
}

$path1 = "C:\My Folder"
$path2 = "C:\My Folder 2"

"hello world" | Out-File -FilePath  "$path1\file.txt"

$job = Start-Job -scriptblock $scriptBlock -ArgumentList $path1, $path2
Wait-Job $job
Receive-Job $job
十级心震 2024-12-31 17:17:31

安迪的伎俩总体上很有效。如果您有参数集,或者想要将复杂的信息移至作业中,您也可以尝试此技术:

$jobArgs = @{Source="foo"; Destination="bar"}
$jobArgs  |Export-CliXml -Path $env:\Temp\MyArgs.clixml

并且在作业中...

Start-Job {
.... $jobArgs =  Import-CliXml -Path $env:\Temp\MyArgs.clixml
} | Wait-Job | Receive-Job

我通常使用这两种方法。

我在以下情况下使用 -ArgumentList / ScriptBlock 参数:

  • 我不处理参数集
  • 我正在使用内存中作业(如 ShowUI 或 WPK 中的 -AsJob 功能),其中参数是真实对象,并且它们不会
  • 消亡在我可以运行作业但无法存储到磁盘(Web 服务器、ISO 兼容实验室等)的用户上下文中运行

如果我需要复杂的参数,并且它们不需要在内存中传递(或其他方式)方便拥有它们稍后在磁盘上),我将使用哈希表方法。

希望这有帮助

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:

$jobArgs = @{Source="foo"; Destination="bar"}
$jobArgs  |Export-CliXml -Path $env:\Temp\MyArgs.clixml

and in the job...

Start-Job {
.... $jobArgs =  Import-CliXml -Path $env:\Temp\MyArgs.clixml
} | Wait-Job | Receive-Job

I use both approaches routinely.

I use -ArgumentList / ScriptBlock parameters when:

  • I am not dealing with parameter sets
  • I'm using an In-Memory Job (like the -AsJob capability in ShowUI or WPK) where the arguments are real objects, and they cannot die
  • I'm running in a user context where I can run a job, but can't store to disk (web servers, ISO compliant labs, etc)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文