proxb / PoshRSJob

Provides an alternative to PSjobs with greater performance and less overhead to run commands in the background, freeing up the console and allowing throttling on the jobs.
MIT License
541 stars 87 forks source link

Add option to filter on 'haserrors' within get-rsjob, or receive-rsjob. #212

Closed TsLan closed 3 years ago

TsLan commented 3 years ago

Do you want to request a feature or report a bug? Feature request:

Add switch behavior to get-rsjob (or receive-rsjob) that can filter on 'HasErrors'

What is the current behavior?

It's not currently possible to use get-rsjob, or receive-rsjob to filter out jobs that are either "false" or "true" on "haserrors".

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem

I'm leveraging poshrsjob to perform a boottime check against a good number of servers. The scriptblock does an IP test to confirm systems are online before the command to check their boottime is run, however errors can occasionally still occur.

If an error occurs within a given job's scriptblock, a subsequent "get-rsjob | receieve-rsjob" will return any errors encountered at the top of the display. Because these errors are part of the jobs, there should IMHO be a native way within Poshrsjob to filter based on 'haserrors'.

What is the expected behavior?

You've got a wonderful module here, it works extremely well. Just feels like their should be more options for filtering on job errors.

Which versions of Powershell and which OS are affected by this issue? Did this work in previous versions of our scripts?

not a version issue.

Please provide a code example showing the issue, if applicable:


#scriptblock called by poshrsjob
$sb=(
    param($S)
    $IPtest=test-connection $S -quiet -count 1
    IF ($IPtest -eq "True")
        {
            $serveronline="1"
        }
    ELSE
        {
            $serveronline="2"
        }
    If ($serveronline -eq "1")
        {
            $lbcheck=Systeminfo /s $S | find /I "Boot Time"
            If (!$lbcheck)
                {
                    $lastboot="No data"
                }
            ELSE
                {
                    $lastboot=$lbcheck.substring(27)
                }
        }
    ELSE
        {
            $lastboot="System unreachable"
        }
    [pscustomobject]@{
    Server = $S
    Lastboot = $lastboot
    }
}
I currently run the following 'extra' steps to clean up job data, by pulling it into an array:

$joblist=get-rsjob
$cleanjobs=@()
$badjobs=@()
$lbrecords=@()
foreach ($record in $joblist)
    {
        $errorcheck=$record.haserrors
        $jid=$record.id
        If ($errorcheck -like $False)
            {
                $cleanjobs+=$jid
            }
        ELSE
            {
                $badjobs+=$jid
            }
    }
foreach ($cj in $cleanjobs)
    {
        $cjid=$cj.id
        $lbrecords+=get-rsjob $cjid | receieve-rsjob
    }
MVKozlov commented 3 years ago

There is no need for this option. There is a much Powershell language solutions, for example Get-RSJob | Where-Object { $_.HasErrors }

or another filter which you like

take a look to https://www.powershellmagazine.com/2014/10/22/foreach-and-where-magic-methods/

Your code example can be rewitten as

$badjobs, $cleanjobs= (Get-RSJob).Where( { $_.HasErrors }, 'Split' )
#or
$grouping = Get-RSJob | Group-Object HasErrors | Sort-Object Name
$grouping[0].Group, $grouping[1].Group
TsLan commented 3 years ago

That's exactly what I was looking to achieve, figured either it was a missed feature, or not needed because of methods I wasn't able to sort out on my own. :)

The samples and examples very much appreciated.