Options
PowerShell Help array of objects
RobertKaucher
Member Posts: 4,299 ■■■■■■■■■■
in Off-Topic
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
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
Any ideas? Has to be something easy that I am just unable to see due to being fixated on the problem.
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
-
Optionsastorrs 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 } }
-
OptionsRobertKaucher 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 } }
-
Optionsastorrs Member Posts: 3,139 ■■■■■■□□□□RobertKaucher wrote: »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] Write-Host $job.fsono $lateJobs += $job } }
As for the correction - it's not like I QA'd it. -
OptionsRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
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. -
OptionsRobertKaucher Member Posts: 4,299 ■■■■■■■■■■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.