【发布时间】: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