Options

PowerShell Help array of objects

RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
I need some help with a problem I am working on.

I have an array of objects called $Jobs. $Jobs has several noteproperties of which the important ones are fsono, fduedate, fcompany.

What I want to do is copy only the items that are late into a new array of objects called $late. I have tried everything that I can think of.

I have tried
$late = new-object System.Object[] $countLate#number of late jobs
for($i = 0; $i -lt $Jobs.Length; $i++)
{
 if($job[$i].fduedate -le $currentDate)
 {
  $late[$x] = $Jobs[$i]
  $x++
 }
}

But that does not give me anything in $late when I do PS>$late[0].fsono

The only work around I have found is to clone it
$late = $Jobs.clone()
And then over write the contents of the $late array doing something like
for($i = 0; $i -lt $Jobs.Length; $i++)
{
 if($job[$i].fduedate -le $currentDate)
 {
  $late[$x] = $Jobs[$i]
  $x++
 }
}
Which I hate...

Any ideas? Has to be something easy that I am just unable to see due to being fixated on the problem.

Comments

  • Options
    astorrsastorrs Member Posts: 3,139 ■■■■■■□□□□
    Forgive me if I've misunderstood but wouldn't this do what you're asking?
    $lateJobs = @()
    foreach ($job in $jobs) {
     if ($job -le $currentDate) {
         $lateJobs += $job
     }
    }
    
  • Options
    RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    I get nothing returned when I run PS C:\>$lateJobs[0].fsono

    Same as in my examples. No error, but no data either.

    And just a quick correction:
    $lateJobs = @()
    foreach ($job in $jobs) {
     if ($job.fduedate -le $currentDate) { #forgot the fduedate [IMG]https://us.v-cdn.net/6030959/uploads/images/smilies/icon_wink.gif[/IMG]
         $lateJobs += $job
     }
    }
    
  • Options
    astorrsastorrs Member Posts: 3,139 ■■■■■■□□□□
    I get nothing returned when I run PS C:\>$lateJobs[0].fsono

    Same as in my examples. No error, but no data either.

    And just a quick correction
    I'm guessing the IF statement is failing. Add a Write-Host to it:
    $lateJobs = @()
    foreach ($job in $jobs) {
     if ($job.fduedate -le $currentDate) { #forgot the fduedate [IMG]https://us.v-cdn.net/6030959/uploads/images/smilies/icon_wink.gif[/IMG]
         Write-Host $job.fsono
         $lateJobs += $job
     }
    }
    

    As for the correction - it's not like I QA'd it. ;)
  • Options
    RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    astorrs wrote: »

    As for the correction - it's not like I QA'd it. ;)

    I know! But people might be looking at it to learn. It was for them, not for you. icon_smile.gif
  • Options
    RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    astorrs wrote: »
    I'm guessing the IF statement is failing. Add a Write-Host to it:

    Yes, this is correct. It's not evaluating as true. But that is odd as I use an if ([datetime]$item.FDueDate -le $currentDate.AddDays(2)) statement just 10 lines up.
  • Options
    astorrsastorrs Member Posts: 3,139 ■■■■■■□□□□
    What does $item.FDueDate.GetType() return?
Sign In or Register to comment.