functions/private/Invoke-ChurchWebRequest.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
function Invoke-ChurchWebRequest {
    param (
        [Parameter(Mandatory = $true)]
        [psobject] $WebSession,
        [Parameter(Mandatory = $true)]
        [string] $Uri,
        [string] $Method,
        [string] $ContentType
    )
    try {
        Write-Host "[INF] Invoking WebRequest...$Uri"
        $params = @{
            Uri             = $Uri
            WebSession      = $WebSession
            UseBasicParsing = $true
            ErrorAction     = "Stop"
        }
        if ($Method) { $params.Add("Method", $Method) }
        if ($ContentType) { $params.Add("ContentType", $ContentType) }
        $response = Invoke-WebRequest @params
        if ($response.Headers.Expires) { throw }
    }
    catch {
        try {
            $response = Invoke-WebRequest @params
        }
        catch {
            $ErrorMessage = $_.Exception.Message
            Write-Host "[ERR] There was an error invoking webrequest $Uri. Exception Message: $ErrorMessage" -ForegroundColor "Red"
        }
    }
    return $response
}