在Powershell {}脚本块中解释变量 [英] Interpret Variable in Powershell { } scriptblock
问题描述
我有一个Shell脚本,应该在后台启动.exe:
I have a shell script which should start a .exe in the background:
$strPath = get-location
$block = {& $strPath"\storage\bin\storage.exe" $args}
start-job -scriptblock $block -argumentlist "-f", $strPath"\storage\conf\storage.conf"
在前面的问题中我发现我需要绝对路径.但是,如果您查看以下命令,则不会解释$ strPath变量:
In a preceding Question I found out that I need absolute Paths. However the $strPath variable isn't interpreted, if you look at the command:
PS Q:\mles\etl-i_test> .\iprog.ps1 --start1
Start Storage
Id Name State HasMoreData Location Command
-- ---- ----- ----------- -------- -------
37 Job37 Running True localhost & $strPath"\storage\bi...
我该如何解决?
我了解我需要将路径作为参数传递,怎么做?像这样:
I understand I need to pass the path as an argument, and how? Something like:
$block = {& $args[0]"\storage\bin\storage.exe" $args[1] $args[2]}
start-job -scriptblock $block -argumentlist $strPath, "-f", $strPath"\storage\conf\storage.conf"
?
推荐答案
脚本块的内容将在PowerShell.exe的另一个实例(作为作业)中执行,该实例无法访问您的变量.这就是为什么您需要在Start-Job
参数列表中发送它们的原因.发送作业将需要用作参数的所有数据.例如,storage.exe的完整路径.
The contents of the script block will be executed in another instance of PowerShell.exe (as the job) which won't have access to your variables. This is why you need to send them in the Start-Job
argumentlist. Send all the data the job will need to function as an argument. The full path to storage.exe for example, e.g.
$path = (Get-Location).Path
$block = {& $args[0] $args[1] $args[2]}
start-job -scriptblock $block -argumentlist `
"$path\storage\bin\storage.exe" `
"-f", `
"$path\storage\conf\storage.conf"
这篇关于在Powershell {}脚本块中解释变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!