简体   繁体   中英

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 .

The input comes from a CSV-file in this format:

\\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. 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?

Thank you for your help guys.

Here is the important part of the script (due to max. 30.000 lines, I limited it):

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:

 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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