Something like this?
$Components = @("virtualserver", "services", "servicegroup", "contentswitch")
$exclusionpattern = @("dev","qa") -join "|"
$netscalerexclusion = @("nsexsrefax6201") -join "|"
$failures = $false
foreach ($component in $Components){
$Metrics = Invoke-RestMethod "https://graphvip100..com/metrics/find?query=metrics.winops.netscalers.components.nsexsre*.$component.*.2m.status" -ErrorAction Stop
$Raw = foreach ($metric in $Metrics){
Invoke-RestMethod "https://graphvip100.com/render?target=$($metric.id)&from=-10min&format=csv" -ErrorAction Stop
}
$Data = $Raw |
ConvertFrom-Csv -Header "Metric","Time","Value" |
Where-Object { -not [string]::IsNullOrEmpty($_.Value) } |
Group-Object -Property Metric
foreach ($group in $Data) {
$NetscalerName = $group.Name.Split(".")[4]
$Componentname = $group.name.split(".")[5]
$Name = $group.Name.Split(".")[6]
If (($group.Group[-1].Value -eq "0.0") -and
($group.Group[-2].Value -eq "0.0") -and
($name -notmatch $exclusionpattern) -and
($netscalername -match $netscalerexclusion)
){
[PSCustomObject]@{
NetscalerName = $NetscalerName
Componentname = $Componentname
Name = $Name
}
$failures = $true
}
ElseIf (($group.Group[-1].Value -eq "1.0") -and
($group.Group[-2].Value -eq "1.0") -and
($name -notmatch $exclusionpattern) -and
($netscalername -match $netscalerexclusion)
){
Write-Verbose "$NetscalerName $Componentname $Name is UP" -Verbose
}
}
} # you can also use "| Export-CSV C:\Errors.csv -notypeinfo" here if you prefer
if ($failures){
Throw "There were errors!"
#OR this:
# Exit 1 # "1" is arbitrary. Choose a numeric value that's meaningful (or not) to you if you need to know why the script failed
}