functions/invoke-psnhttpendpoint.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
<#
.SYNOPSIS
Invoke a HTTP Endpoint
 
.DESCRIPTION
Invoke a HTTP Endpoint and send a payload along
 
.PARAMETER Url
The URL endpoint that is capable of handling your request
 
The URL parameter has a default value if you have configured one with the designated functions
 
.PARAMETER Payload
The raw (string) payload that you want to pass along to the HTTP Endpoint
 
.PARAMETER Type
Type parameter to instruct the HTTP Endpoint what kind of data that will be part of the request
 
.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
$hashTable = @{email = "admin@domain.com"; subject = "More testing"; message = "I hope this finds you well"}
$payload = ($hashTable | ConvertTo-Json)
 
Invoke-PSNHttpEndpoint -Payload $payload -Url "https://prod-35.westeurope.logic.azure.com:443/workflows/14adfasdrae23354432636dsfasfdsaf/"
 
This will invoke the HTTP endpoint and send along a payload. The payload comes from the HashTable that is converted into a Json object.
 
.NOTES
 
Author: Mötz Jensen (@Splaxi)
 
#>

function Invoke-PSNHttpEndpoint {
    [CmdletBinding()]
    param (
        [string] $Url = (Get-PSNUrl).Url,

        [Parameter(Mandatory = $True)]
        [string] $Payload,

        [ValidateSet('Json')]
        [string] $Type = "Json",

        [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}
        
        Write-PSFMessage -Level Verbose -Message "Prepping the details for executing the HTTP request."

        [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
        $request = [System.Net.WebRequest]::Create($Url)
        $request.Method = "POST"
        $request.ContentType = "application/$($Type.ToLower())";

        try {
            $stream = new-object System.IO.StreamWriter ($request.GetRequestStream())
            $stream.write($Payload)
            $stream.Flush()
            $stream.Close()

            Write-PSFMessage -Level Verbose -Message "Executing the HTTP request."
            $response = $request.GetResponse()
    
            $requestStream = $response.GetResponseStream()
            $readStream = New-Object System.IO.StreamReader $requestStream
            $data = $readStream.ReadToEnd()
        }
        catch {
            Write-PSFMessage -Level Verbose -Message "Something went wrong while contacting the http endpoint." -ErrorRecord $_
        }
        
        $data
    }
    
    end {
    }
}