Array suppressing output of another one???

Hi everyone,

I have below code, I am seeing a problem that one of these variables output – $FailedErrorStatus , $FailedObjectStatus is being suppressed if both of them have some content inside, but if I put the | out-host after each variable, I see correct input, any ideas? I need this to work without out-host, out-string, write-output

Function Find-AzAutomationFailedJob { [CmdletBinding()] param ( [Parameter(Mandatory=$false)] [ValidateNotNullOrEmpty()] [int]$DaysBack = 1 ) process { $Today = [datetime]::Now $FindAutomationAccounts = (Get-AzAutomationAccount) $FailedObjectStatus = [System.Collections.ArrayList]::new() $FailedErrorStatus = [System.Collections.ArrayList]::new() foreach ($Account in $FindAutomationAccounts) { $FindJobs = (Get-AzAutomationJob -ResourceGroupName $($Account.ResourceGroupName) -AutomationAccountName $($Account.AutomationAccountName) ` | where-object {$_.EndTime -gt $($Today.AddDays(-$DaysBack))}) if ($FindJobs) { foreach ($Job in $FindJobs) { if ($Job.Status -eq "Failed") { $FailedObject = [PSCustomObject]@{ AutomationAccount = $Job.AutomationAccountName Runbook = $Job.RunbookName Status = $Job.Status } [void]$FailedObjectStatus.Add($FailedObject) } else { $FindError = (Get-AzAutomationJobOutput -Id $Job.JobId -ResourceGroupName $Job.ResourceGroupName -AutomationAccountName $Job.AutomationAccountName) ` | Where-Object {$_.Type -eq "Error"} if ($FindError) { $ErrorObject = [PSCustomObject]@{ JobId = $FindError.JobId Type = $FindError.Type } [void]$FailedErrorStatus.Add($ErrorObject) } } } } } ##### PROBLEM HERE ##### if ($FailedErrorStatus) { $FailedErrorStatus } if ($FailedObjectStatus) { $FailedObjectStatus } } } 

EDIT – This is how I solved it 🙂 – Thanks for the input guys

Function Find-AzAutomationFailedJob { [CmdletBinding()] param ( [Parameter(Mandatory=$false)] [ValidateNotNullOrEmpty()] [int]$DaysBack = 1 ) process { $Today = [datetime]::Now Confirm-AzSession $FindAutomationAccounts = (Get-AzAutomationAccount) $FailedObjectStatus = [System.Collections.ArrayList]::new() foreach ($Account in $FindAutomationAccounts) { $FindJobs = (Get-AzAutomationJob -ResourceGroupName $($Account.ResourceGroupName) -AutomationAccountName $($Account.AutomationAccountName) ` | where-object {$_.EndTime -gt $($Today.AddDays(-$DaysBack))}) if ($FindJobs) { foreach ($Job in $FindJobs) { if ($Job.Status -eq "Failed") { $FailedObject = [PSCustomObject]@{ AutomationAccount = $Job.AutomationAccountName Runbook = $Job.RunbookName Status = $Job.Status JobId = $Job.JobId Type = "Job-Failed" } $FailedObjectStatus += $FailedObject } if ($Job.Status -ne "Failed") { $FindError = (Get-AzAutomationJobOutput -Id $Job.JobId -ResourceGroupName $Job.ResourceGroupName -AutomationAccountName $Job.AutomationAccountName) ` | Where-Object {$_.Type -eq "Error"} if ($FindError) { $FailedObjectStatus += [PSCustomObject]@{ AutomationAccount = $FindError.AutomationAccountName Runbook = $Job.RunbookName Status = $Job.Status JobId = $FindError.JobId Type = "Job-Produced-Errors" } } } } } } if ($FailedObjectStatus) { $FailedObjectStatus } } } 

submitted by /u/nemanja_jovic
[link] [comments]

Leave a Reply