functions/invoke-psnmessage.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<#
.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
 
.PARAMETER AsJob
Switch to instruct the cmdlet to start a bacground job and make sure the execution isn't blocked while contacting the HTTP endpoint
 
.PARAMETER JobName
Name for the background job that will be started
 
.PARAMETER EnableException
This parameters disables user-friendly warnings and enables the throwing of exceptions.
This is less user friendly, but allows catching exceptions in calling scripts.
 
.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 styled message / payload. The notification will be sent to "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 styled message / payload. The notification details comes from the HashTable that is converted into a Json object.
 
.EXAMPLE
Set-PSNUrl -Url "https://prod-35.westeurope.logic.azure.com:443/workflows/14adfasdrae23354432636dsfasfdsaf/"
Invoke-PSNMessage -ReceiverEmail "admin@domain.com" -Subject "Testing from the new module" -Message "This should arrive at your door steps"
 
This will invoke the default HTTP endpoint that has been configured for the module and send of a notification styled message / payload. The notification will be sent to "admin@domain.com", with the Subject "Testing from the new module" and the Message "This should arrive at your door steps".
 
.EXAMPLE
Invoke-PSNMessage -ReceiverEmail "admin@domain.com" -Subject "Testing from the new module" -Message "This should arrive at your door steps" -AsJob
 
This will invoke the default HTTP endpoint that has been configured for the module and send of a notification styled message / payload. The notification will be sent to "admin@domain.com", with the Subject "Testing from the new module" and the Message "This should arrive at your door steps". The execution is done in a background job and will not block the execution.
 
.NOTES
 
Author: Mötz Jensen (@Splaxi)
 
#>

function Invoke-PSNMessage {
    [CmdletBinding(DefaultParameterSetName = 'Specific')]
    param (
        [string] $Url = (Get-PSNUrl).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 = "HttpEndpointInvocation",

        [switch] $EnableException

    )

    begin {
        if(-not $Url) {
            Write-PSFMessage -Level Warning -Message "It seems that you didn't pass a URL and the module doesn't have a configured one to use."
            Stop-PSFFunction -Message "Stopping because of missing URL" -EnableException $EnableException
            return
        }

    }
    
    process {
        if(Test-PSFFunctionInterrupt) {return}

        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 -Name $JobName -ScriptBlock {
                param($Parameters)
                Import-Module PSNotification -Force -PassThru
                Invoke-PSNHttpEndpoint @Parameters
             } -ArgumentList $Arguments
        }
        else {
            Invoke-PSNHttpEndpoint -Url $Url -Payload $RequestData
        }
    }
    
    end {
    }
}