functions/invoke-psnmessage.ps1
<#
.SYNOPSIS Send a notification message .DESCRIPTION Send a message styled notification .PARAMETER Url The URL endpoint that is capable of handling your request .PARAMETER ReceiverEmail The email address of the receiver that you want get the notification .PARAMETER Subject The subject of the notification that you want to send .PARAMETER Message The body/message of the notification that you want to send .PARAMETER Json The raw Json object that you want to pass .EXAMPLE Invoke-PSNMessage -ReceiverEmail "admin@domain.com" -Subject "Testing from the new module" -Message "This should arrive at your door steps" -Url "https://prod-35.westeurope.logic.azure.com:443/workflows/14adfasdrae23354432636dsfasfdsaf/" This will invoke the HTTP endpoint and send of a notification. The notification will be sent for "admin@domain.com", with the Subject "Testing from the new module" and the Message "This should arrive at your door steps". .EXAMPLE $hashTable = @{email = "admin@domain.com"; subject = "More testing"; message = "I hope this finds you well"} $payload = ($hashTable | ConvertTo-Json) Invoke-PSNMessage -Json $payload -Url "https://prod-35.westeurope.logic.azure.com:443/workflows/14adfasdrae23354432636dsfasfdsaf/" This will invoke the HTTP endpoint and send of a notification. The notification details comes from the HashTable that is converted into a Json object. .NOTES Author: Mötz Jensen (@Splaxi) #> function Invoke-PSNMessage { [CmdletBinding(DefaultParameterSetName = 'Specific')] param ( [string] $Url = $Script:Url, [Parameter(Mandatory = $True, ParameterSetName = 'Specific')] [Alias('Email')] [string] $ReceiverEmail, [Parameter(Mandatory = $True, ParameterSetName = 'Specific')] [string] $Subject, [Parameter(Mandatory = $True, ParameterSetName = 'Specific')] [string] $Message, [Parameter(Mandatory = $True, ParameterSetName = 'Json')] [Alias('Payload')] [string] $Json, [switch] $AsJob, [string] $JobName ) begin { } process { if ($PSCmdlet.ParameterSetName -eq "Json") { Write-PSFMessage -Level Verbose -Message "The execution is a Json payload passed directly." $RequestData = $Json } else { Write-PSFMessage -Level Verbose -Message "The execution is a specific parameters passed." $RequestData = "{`"email`":`"$ReceiverEmail`", `"message`":`"$Message`", `"subject`":`"$Subject`"}" } if($AsJob.IsPresent) { $Arguments = @{Url = $Url; Payload = $RequestData} Start-RSJob -ScriptBlock { param($Parameters) Import-Module PSNotification -Force -PassThru Invoke-PSNHttpEndpoint @Parameters } -ArgumentList $Arguments } else { Invoke-PSNHttpEndpoint -Url $Url -Payload $RequestData } } end { } } |