【发布时间】:2020-04-14 20:12:13
【问题描述】:
我有一个导入模块 \path\to\functions.psm1 的主脚本, 我在主脚本中调用了一个名为:Run_Scans $ServerList 的函数,它来自functions.psm1, 从 Run_Scans 我尝试使用 Start-Job 调用另外两个函数(Scan1、Scan2),但无法让服务器变量传递给其他函数。我尝试了几种不同的方法,无论是作业失败还是完成,但服务器列表确实通过了。任何帮助将不胜感激。
#This is from the main script#
Import-Module "path\to\Functions.psm1"
Run_Scans $ServerList
#This is from functions.psm1#
Function Run_Scans ($Servers){
#Start-Job -Name 'Scan1' -ScriptBlock {Run_Scan1 $using:Servers}
#Start-Job -Name 'Scan2' -ScriptBlock {Run_Scan2 $using:Servers}
$Scan = {
param ($Servers)
Run_Scan1 $Servers
Run_Scan2 $Servers
}
Start-Job -ScriptBlock $Scan -ArgumentList $Servers
}
Function Run_Scan1 ($Servers){
scan code
}
Function Run_Scan2 ($Servers){
scan code
}
更新:
我能够做到以下几点:
$ServerList = "Serv1","Serv2","Serv3"
Start-Job -InitializationScript $Initialization -ScriptBlock {param($servers) echo $servers} -args (,$servers)
Receive-Job
输出:
Serv1
Serv2
Serv3
但是当我尝试合并调用函数时:
$Initialization = [scriptblock]::Create("Import-Module -Name 'Pathto\Functions.psm1'")
Start-Job -InitializationScript $Initialization -ScriptBlock {param($servers) Run_Scan1 $servers} -args (,$servers)
我收到以下错误:
术语“Run_Scan1”未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。
无法索引到空数组。
最终解决方案:
Start-Job -InitializationScript {Import-Module "Pathto\Functions.ps1"} -ScriptBlock { param($Servers) Run_Scan1 $Servers } -ArgumentList (,$servers)
【问题讨论】:
-
后台作业在一个独立的 PowerShell 子进程 的会话中运行,该进程对调用者的状态一无所知,因此
Run_Scan1和Run_Scan2函数不会'不能在那里。除此之外,如果$Servers是一个数组,您必须将其作为-ArgumentList (, $Servers)传递,如 js2010 的答案所示。 -
如果您使用 -InitializationScript 导入模块不会使该功能可用吗?
-
是的,应该可以;请注意,在 PS 6 中,后台作业在
$HOME\Documents中运行,而不是在调用者的当前目录中,因此请务必在Pathto\Functions.psm1中考虑这一点 - 请参阅 this answer。 -
感谢您的帮助
标签: function powershell start-job