将变量传递给 Start-Job [英] Passing Variables to Start-Job

查看:91
本文介绍了将变量传递给 Start-Job的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在这里遗漏一些非常简单的东西.这是一个非常基本的脚本来说明我正在尝试的内容:

I have to be missing something incredibly simple here. Here's a very basic script to illustrate what I'm trying:

$Computers = @('comp1', 'comp2')

$ScriptBlock = {
    New-Item "C:\Temp\$C.txt" -Force
}

Foreach ($C in $Computers)
{
    Start-Job -ScriptBlock $ScriptBlock -ArgumentList $C
}

脚本运行,但 $C 没有通过,所以我的文件夹中只有一个.txt"文件.我在这里忽略了什么简单的事情?

The script runs, but $C is not passed, so i just get a ".txt" file in my folder. What simple thing am I overlooking here?

推荐答案

替换为:

$ScriptBlock = {
    New-Item "C:\Temp\$C.txt" -Force
}

有了这个:

$ScriptBlock = 
{
    param($C)
    New-Item "C:\Temp\$C.txt" -Force
}

注意:当您在参数列表中传递参数时,请确保使用 param 在脚本块内必须接受相同数量的参数.

Note: When you are passing the arguments in the argumentlist , then make sure the same number of arguments have to accepted inside the scriptblock by using param.

这篇关于将变量传递给 Start-Job的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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