Share via

Powershell alert mail

Sara 441 Reputation points
2023-07-11T15:09:29.2033333+00:00

I am just trying to add an additional lines to send an email to an DL when the "$R.status is down and $R status is resolved, I am just not getting the syntax correctly on where I can use the below lines to send an email on the below PS script. can someone guide me through that?

[CmdletBinding()]
Param (
	[string[]]$To = @("abc.com")
)

# Mail it out
$MailSplat = @{
    To         = $To
    From       = "no.com"
    Subject    = $json
    Body       = $json.summary
    SMTPServer = "hub.com"
    BodyAsHtml = $true
}

Send-MailMessage @MailSplat
$VIPResults = Get-NSVIPAlertingStatus

if ($null -ne $VIPResults)
{
    foreach ($R in ($VIPResults | Where-Object Status -notmatch "Up|Out Of Service"))
    {
        if ($R.status -eq "Down")
        {
            $Stauts = $R.Status
            $Severity = "5"
        }
        Elseif ($R.status -eq "Resolved")
        {   
            $Stauts = $R.Status
            $Severity = "0"
        }

        Write-Verbose "$(Get-Date): Send Alert: Netscaler $($R.name) on $($R.netscalername) is $($R.Status)" -Verbose
        $JSON = @"
        {
            "summary": "Netscaler $($R.name) on $($R.netscalername) is $($R.Status)",
            "node": "$($R.netscalername)",
            "team": "",
            "severity": $Severity,
            "sop_link": "",
            "category": "Netscaler component $($R.name)",
            "integration": "" 
        }
"@
        
        Write-Verbose "$(Get-Date):se" -Verbose
        $Json
      Invoke-WebRequest -Uri http:\\test.com -Method Post -Body \$Json -ContentType application/json -Verbose
 
      
    }
}
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

2 answers

Sort by: Most helpful
  1. Rich Matheisen 48,116 Reputation points
    2023-07-12T14:49:38.4266667+00:00

    Changing from a function to in-line code is something you could have easily done yourself.

    $VIPResults = Get-NSVIPAlertingStatus
    
    if ($null -ne $VIPResults) {
        foreach ($R in ($VIPResults | Where-Object Status -NotMatch "Up|Out Of Service")) {
            if ($R.status -eq "Down") {
                $Stauts = $R.Status         # "status" is misspelled
                $Severity = "5"
            }
            Elseif ($R.status -eq "Resolved") {   
                $Stauts = $R.Status         # "status" is misspelled
                $Severity = "0"
            }
    
            Write-Verbose "$(Get-Date): Send Alert: Netscaler $($R.name) on $($R.netscalername) is $($R.Status)" -Verbose
            $JSON = @"
            {
                "summary": "Netscaler $($R.name) on $($R.netscalername) is $($R.Status)",
                "node": "$($R.netscalername)",
                "team": "",
                "severity": $Severity,
                "sop_link": "",
                "category": "Netscaler component $($R.name)",
                "integration": "" 
            }
    "@
            
            Write-Verbose "$(Get-Date):se" -Verbose
            # Mail it out
            $To = "******@abc.com"
            $MailSplat = @{
                To         = $To
                From       = "******@no.com"
                Subject    = "Netscaler $($R.name) on $($R.netscalername) is $($R.Status)"
                Body       = $JSON
                SMTPServer = "hub.com"
                BodyAsHtml = $true
            }
            Send-MailMessage @MailSplat
            Invoke-WebRequest -Uri http:\\test.com -Method Post -Body \$Json -ContentType application/json -Verbose
        }
    }
    

    Was this answer helpful?

    0 comments No comments

  2. Rich Matheisen 48,116 Reputation points
    2023-07-11T18:36:15.64+00:00

    The stated goal is clear (too send an email), but how (in-line or function) and what (just the status for a single netscaler?) isn't.

    Here's one way:

    Function SendAnEmail {
        [CmdletBinding()]
        Param (
            [string[]]$To = @("abc.com"),
            $Json
        )
        $status = ConvertFrom-Json $Json
    
        # Mail it out
        $MailSplat = @{
            To         = $To
            From       = "no.com"
            Subject    = "Netscaler status"
            Body       = $status.summary
            SMTPServer = "hub.com"
            BodyAsHtml = $true
        }
    
        Send-MailMessage @MailSplat
    }
    ####################
    $VIPResults = Get-NSVIPAlertingStatus
    
    if ($null -ne $VIPResults) {
        foreach ($R in ($VIPResults | Where-Object Status -NotMatch "Up|Out Of Service")) {
            if ($R.status -eq "Down") {
                $Stauts = $R.Status         # "status" is misspelled
                $Severity = "5"
            }
            Elseif ($R.status -eq "Resolved") {   
                $Stauts = $R.Status         # "status" is misspelled
                $Severity = "0"
            }
    
            Write-Verbose "$(Get-Date): Send Alert: Netscaler $($R.name) on $($R.netscalername) is $($R.Status)" -Verbose
            $JSON = @"
            {
                "summary": "Netscaler $($R.name) on $($R.netscalername) is $($R.Status)",
                "node": "$($R.netscalername)",
                "team": "",
                "severity": $Severity,
                "sop_link": "",
                "category": "Netscaler component $($R.name)",
                "integration": "" 
            }
    "@
            
            Write-Verbose "$(Get-Date):se" -Verbose
            SendAnEmail -Json $JSON
            Invoke-WebRequest -Uri http:\\test.com -Method Post -Body \$Json -ContentType application/json -Verbose
     
          
        }
    }
    

    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.