-2

I have a script, where I get my output using pscustom object (the one with the value -eq 0.0) but is there a way to join $netscaler name+$cmponentname+$name in a single variable ?


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

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))
        {
           $vipdown = [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
        }
        ElseIf (($group.Group[-1].Value -eq "2.0") -and ($group.Group[-2].Value -eq "2.0") -and ($name -notmatch $exclusionpattern) -and ($netscalername -match $netscalerexclusion))
        {
            Write-Verbose "$NetscalerName $Componentname $Name is OUT OF SERVICE" -Verbose
        }
    }   
}   
if ($failures){ 
    Throw "Issue with netsclaer!"
}

By storing it in a single variable, I am trying to get the data and use it something like below,

Try{
    $VIPResults = get-NSVIPStatus
}
Catch{
    Throw $_
}
if ($null -ne $VIPResults)
{
    foreach ($R in $Results)
    {
        Write-Verbose "$(Get-Date): Send NOC Alert: EXSRE Netscaler $($R.name) on $($R.netscalername) is down" -Verbose

2 Answers2

0

If you do not output anything else in your loop, you can assign that output to a variable:

$MyPSCustomObjects = foreach (...) {
    [PSCustomObject]@{...}
}

Otherwise, you can just collect those objects in an ArrayList:

$MyPSCustomObjects = New-Object -TypeName "System.Collections.ArrayList"
foreach (...) {
    $MyPSCustomObjects.Add([PSCustomObject]@{...})
}
stackprotector
  • 10,498
  • 4
  • 35
  • 64
  • Little new, but still how do I get this stored in the variable $vipstatus and also display the output $VIPstatus = New-Object System.Collections.ArrayList($null) $null = $VIPstatus.Add($vipdown) – saravana raj Jun 21 '23 at 07:38
0

This worked, ^ Thanks @stackprotector

$VIPstatus = New-Object System.Collections.ArrayList($null)

 foreach ($vip in $vipdown)
    {
        $null = $VIPstatus.Add($vipdown)
    }