Share via

powershell

Sara 441 Reputation points
2023-06-17T16:29:05.35+00:00

I have a PS script where I it to convert it as a function to use that in a job to trigger alerts.

  1. I want the script to be converted as a function and output to be stored in a variable.GetVIPSTATUS.txt
  2. How To use that function in a job using that variables. Ex: job.txt
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-17T19:36:15.2733333+00:00

Here's the job (I also corrected the quoting in the here-string):


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
        $JSON = @"
        {
        'summary': 'EXSRE Netscaler $($_.name) on $($_.netscalername) is down',
        'node': '$($_.netscalername)',
        'team': 'EASRE',
        'severity': '5',
        'sop_link': '',
        'category': 'Netscaler component',
        'integration': 'EXSRE Jenkins'
        }
"@
        Write-Verbose "$(Get-Date): Payload being sent to the NOC" -Verbose
        $Json
    }


Here's the function:

function get-NSVIPStatus{
    $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
            }
            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 EASREnetsclaer!"
    }
}


Here's the function:

function get-NSVIPStatus{
    $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
            }
            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 EASREnetsclaer!"
    }
}

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most 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.