将字符串参数发送到Start-Job脚本块 [英] Send string parameters to a Start-Job script block

查看:63
本文介绍了将字符串参数发送到Start-Job脚本块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用shell初始化作业.该工作将是一个延迟,再加上对vbScript的调用.以下代码可以正常工作.在我的示例中,vbScript只是一行,其中包含 MsgBox"Hello world!"

I need to initialize a job using the shell. The job will be a delay plus a call to a vbScript. The following code works fine. For my example, the vbScript is just a single line with a 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路径的时刻.(这个想法是对一些不同的vbScript进行几个不同的调用).当我这样做时,该命令似乎忽略了参数输入.我使用 int 参数进行了其他测试,但它们工作正常,问题似乎仅在于 string 参数.以下代码不起作用:

The problem comes the moment I want to parameterize the vbScript path. (the idea is to do several different calls to some different vbScripts). When I do this, the command seems to ignore the parameter input. I did other tests with int parameter and they work fine, the problem looks to be only with the string parameters. The following code does not work:

$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]命令,但是它有同样的问题.

I've also tried using the [-ArgumentList] command, but it has the same problem.

有什么主意吗?

推荐答案

问题是脚本块中的 $ input $ seconds 变量位于不同的位置范围,并且实际上是与主脚本中的变量不同的变量.

The problem is that the $input and $seconds variables inside your script block are in a different scope and are effectively different variables to the ones in the main script.

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

I've modified your script slightly to remove the call to VBScript to make it easier to reproduce here - my example code is:

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

有两种解决方法:

使用中:范围修改器

请参见

See https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_scopes?view=powershell-7#the-using-scope-modifier for the full details, but basically:

对于在会话外执行的任何脚本或命令,您需要使用作用域"修饰符从调用会话作用域中嵌入变量值,以便会话外代码可以访问它们.

For any script or command that executes out of session, you need the Using scope modifier to embed variable values from the calling session scope, so that out of session code can access them.

$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 -这使您可以注入"或删除脚本.将主脚本中的变量放入脚本块.

Note the $using before the variable names inside the script block - this allows you to "inject" the variables from your main script into the scriptblock.

ScriptBlock参数

您可以在脚本块上定义与使用函数相似的参数,然后在调用 Start-Job -ArgumentList 参数中提供值>.

You can define parameters on the script block similar to how you do it with a function, and then provide the values in the -ArgumentList parameter when you invoke Start-Job.

$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'
``

这篇关于将字符串参数发送到Start-Job脚本块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆