【问题标题】:Send string parameters to a Start-Job script block将字符串参数发送到 Start-Job 脚本块
【发布时间】:2020-12-21 07:13:51
【问题描述】:

我需要使用 shell 初始化作业。这项工作将是一个延迟加上对 vbScript 的调用。以下代码工作正常。对于我的示例,vbScript 只是带有 MsgBox "Hello world!" 的单行代码

$functions = {
    Function execute_vbs {
        param ([string]$path_VBScript, [int]$secs)
        Start-Sleep -Seconds $secs
        cscript /nologo $path_VBScript 
    }
}
$seconds = 2
Start-Job -InitializationScript $functions -ScriptBlock {execute_vbs -path_VBScript 'C:\Users\[USERNAME]\Desktop\hello_world.vbs' -secs $seconds} -Name MyJob

问题出现在我想参数化 vbScript 路径的那一刻。 (这个想法是对一些不同的 vbScripts 进行几次不同的调用)。 当我这样做时,该命令似乎忽略了参数输入。我用int 参数做了其他测试,它们工作正常,问题似乎只出在string 参数上。以下代码不起作用:

$functions = {
    Function execute_vbs {
        param ([string]$path_VBScript, [int]$secs)
        Start-Sleep -Seconds $secs
        cscript /nologo $path_VBScript 
    }
}
$input = 'C:\Users\[USERNAME]\Desktop\hello_world.vbs'
$seconds = 2
Start-Job -InitializationScript $functions -ScriptBlock {execute_vbs -path_VBScript $input -secs $seconds} -Name MyJob

我也尝试过使用 [-ArgumentList] 命令,但它有同样的问题。

有什么想法吗?

【问题讨论】:

  • 这能回答你的问题吗? Passing Variables to Start-Job
  • 并非如此。在我的情况下,解决方案是在每个变量之前包含 $using:,但无论如何感谢 ;-)
  • 这能回答你的问题吗? Passing arguments to Start-Job scriptblock?
  • 不管怎样都是重复的,之前已经被报道过多次了。只需要发帖前搜索一下。
  • 我之前没有发现这个问题,我已经搜索过了。它看起来很相似,但这个问题没有回答我的疑问

标签: powershell batch-file vbscript


【解决方案1】:

问题在于脚本块中的 $input$seconds 变量处于不同的范围内,并且实际上是与主脚本中的变量不同的变量。

我已经稍微修改了您的脚本以删除对 VBScript 的调用,以便更容易在此处重现 - 我的示例代码是:

$functions = {
    Function execute_vbs {
        param ([string]$path_VBScript, [int]$secs)
        Start-Sleep -Seconds $secs
        write-output "filename = '$path_VBScript'"
        write-output "secs = '$secs'"
    }
}

这里有两种修复方法:

使用:范围修饰符

请参阅https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7#the-using-scope-modifier 了解完整详情,但基本上:

对于在会话外执行的任何脚本或命令,您需要使用范围修饰符来嵌入来自调用会话范围的变量值,以便会话外代码可以访问它们。

$filename = 'C:\Users\[USERNAME]\Desktop\hello_world.vbs'
$seconds = 2

$job = Start-Job -InitializationScript $functions -ScriptBlock {
        execute_vbs -path_VBScript $using:filename -secs $using:seconds
    } -Name MyJob

wait-job $job

receive-job $job
# output:
# filename = 'C:\Users\[USERNAME]\Desktop\hello_world.vbs'
# secs = '2'

注意脚本块内变量名前的$using - 这允许您将主脚本中的变量“注入”到脚本块中。

ScriptBlock 参数

您可以在脚本块上定义参数,类似于使用函数的方式,然后在调用Start-Job 时提供-ArgumentList 参数中的值。

$filename = 'C:\Users\[USERNAME]\Desktop\hello_world.vbs'
$seconds = 2

$job = Start-Job -InitializationScript $functions -ScriptBlock {
    param( [string] $f, [int] $s )
    execute_vbs -path_VBScript $f -secs $s
} -ArgumentList @($filename, $seconds) -Name MyJob

wait-job $job

receive-job $job
# output:
# filename = 'C:\Users\[USERNAME]\Desktop\hello_world.vbs'
# secs = '2'
``

【讨论】:

  • 我是如此接近!呵呵呵呵。我对这种编程语言很陌生。非常感谢!!我花了大约 14 个小时才来到论坛:P
猜你喜欢
  • 2013-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-20
  • 1970-01-01
相关资源
最近更新 更多