简体   繁体   English

PowerShell调用命令-AsJob

[英]PowerShell Invoke-Command -AsJob

The script I wrote is pretty long but works fine except for one problem. 我写的脚本很长,但除了一个问题外,工作正常。 When I have more jobs for the same $Server it's only submitting one job and doesn't execute the second or third job for the same $Server . 当我在同一个$Server有更多jobs ,它只提交一个工作,而不对同一个$Server执行第二个或第三个工作。

The input comes from a CSV-file in this format: 输入来自CSV文件,格式为:

\\domain\SHARE\Target3, 0
BELSFBRUS0131, E:\DEPARTMENTS\CBR\SHARE\Target2, 0
BELSFBRUS0131, E:\DEPARTMENTS\CBR\SHARE\Target4, 0

In the example above the script only starts 2 jobs, one for the UNC-path Target3 and one for Target2 on BELSFBRUS0131 , these are executed fine. 在脚本上面的例子只启动2组的工作,一个用于UNC路径Target3 ,一个用于Target2BELSFBRUS0131 ,这些执行精细。 But there's no job launched for Target4 to the same server... Is this maybe a restriction of Invoke-Command to only launch once to each server? 但是没有为同一服务器上的Target4启动任何作业...这是否可能是Invoke-Command的限制,使其只能在每个服务器上启动一次?

Thank you for your help guys. 谢谢您的帮助。

Here is the important part of the script (due to max. 30.000 lines, I limited it): 这是脚本的重要部分(由于最多30.000行,我限制了它):

Foreach ($_ in $File) {

# Starting jobs

    if (($Server -eq "UNC") -or ($Server -eq "$env:COMPUTERNAME")) {
        Write-Host "$(Get-TimeStamp) $env:COMPUTERNAME > Start job: local > $Server, $Target, $OlderThanDays, $LogFolder, $CleanFolders" -ForegroundColor Gray
        $arrayAllJobs += Start-Job -ScriptBlock $JobCall -ArgumentList ($Target, $OlderThanDays, $Server, $LogFolder, $CleanFolders) -Name DelFiles
    }
    else {
          Write-Host "$(Get-TimeStamp) $env:COMPUTERNAME > Start job: Remote > $Server, $Target, $OlderThanDays, $LogFolder, $CleanFolders" -ForegroundColor Gray        
          $arrayAllJobs += Invoke-Command -ScriptBlock $JobCall -ArgumentList ($Target, $OlderThanDays, $Server, $LogFolder, $CleanFolders) -ComputerName "$Server.grouphc.net" -Authentication Credssp -Credential $Credentials -AsJob -JobName DelFiles
    }  
}
#__________________________________________________________________________________________________________________________________
# Checking jobs

Write-Host "`n$(Get-TimeStamp) $env:COMPUTERNAME > Waiting for all jobs to finish.." -ForegroundColor Cyan; Wait-Job -Job $arrayAllJobs
Write-Host "`n$(Get-TimeStamp) $env:COMPUTERNAME > The targets were:" -ForegroundColor Cyan; $arrayAllPaths | Format-List

foreach ($Job in $arrayAllJobs) {
    if ($job.State -ne 'Completed') {
        # Write-Host ($Job.ChildJobs[0].JobStateInfo.Reason.Message) -ForegroundColor Red
        $arrayJobError += $Job.ChildJobs[0].JobStateInfo.Reason.Message
        $HTMLarrayJobError += $Job.ChildJobs[0].JobStateInfo.Reason.Message +"<br>"
    } 
    #<# Reported success:
    else {
        Write-Host (Receive-Job $Job) -ForegroundColor Green 
    } #>
}

I suspect it related to the fact that all of your jobs are being created with the same JobName: 我怀疑这与您使用相同的JobName创建所有作业的事实有关:

 Invoke-Command -ScriptBlock $JobCall -ArgumentList ($Target, $OlderThanDays, $Server, $LogFolder, $CleanFolders) -ComputerName "$Server.grouphc.net" -Authentication Credssp -Credential $Credentials -AsJob -JobName DelFiles

so you're trying to create two jobs with the same name, on the same server. 因此,您尝试在同一台服务器上创建两个具有相同名称的作业。

I made a previous error in my script, that was the reason why the seconde server didn't arrive in the loop. 我在脚本中犯了一个先前的错误,这就是第二服务器未进入循环的原因。 I'm sorry everyone, my bad. 对不起大家,我不好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM