Share via

Throw and for each

Sara 441 Reputation points
2023-06-14T14:36:14.49+00:00

I have a script and I am trying it to "throw" as an issue if it detects the component as down and if not run through the script as successful.

While I try to do that my throw statement errors out and stops looping through with the below error msg, Any thoughts on how to loop through the rest of the components which are down and I want it to throw an issue with each component separately.

06/14/2023 09:07:00: Issue with (nsexs6201 virtualserver brc_test) is down At D:\netscalertest\get-nsstats.ps1:28 char:13 + Throw "$(get-date): Issue with ($NetscalerName $Component ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (06/14/2023 09:0...c_test) is down:String) [], RuntimeException + FullyQualifiedErrorId : 06/14/2023 09:07:00: Issue with (nsexs6201 virtualserver brc_test) is down

$Components = @("virtualserver", "services", "servicegroup", "contentswitch")

$exclusionpattern = @("dev","qa") -join "|"
$netscalerexclusion = @("nsexsrefax6201") -join "|"

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))
        {
Throw "$(get-date): Issue with ($NetscalerName $Componentname $Name) is down"
    }
        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
        }
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author

Rich Matheisen 48,116 Reputation points
2023-06-15T15:12:55.22+00:00

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
}

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Rich Matheisen 48,116 Reputation points
    2023-06-14T18:51:16.7733333+00:00

    "Throw" causes a terminating exception. That's what's causing your problem.

    Is it just your intention to display an error message? If so, use Write-Error cmdlet which causes a non-terminating exception.

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.