-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInvoke-AzureAutomationWebHook.ps1
64 lines (52 loc) · 2.39 KB
/
Invoke-AzureAutomationWebHook.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
<#
.SYNOPSIS
Invokes an Azure Automation Webhook that requires no parameters to be passed during run-time. Likely you want to hard-code the params into this or call it from another script.
.EXAMPLE
Configure $uri/-Uri to be the Webhook URI, and $ExpiryDateTime/-ExpiryDateTime to be the time when the Webhook will no longer operate so that you're aware of why it fails when it inevitably does expire.
.\Invoke-AzureAutomationWebHook.ps1 -Uri https://example.com/webhook/uri -ExpiryDateTime '1985-11-09 19:05'
.NOTES
Author: ashley.geek.nz
Version: 2016-11-24 1950
Github: https://github.com/webash/azure-automation/
Reference: https://azure.microsoft.com/en-gb/documentation/articles/automation-webhooks/
.LINK
https://github.com/webash/azure-automation/
#>
param(
[ValidateNotNullOrEmpty()][string]$uri = '',
[ValidateNotNullOrEmpty()][string]$ExpiryDateTime = "1970-01-01 00:01"
);
function AreYouSure {
# Keep window open on failure from: http://blog.danskingdom.com/keep-powershell-console-window-open-after-script-finishes-running/
if ($Host.Name -eq "ConsoleHost")
{
Write-Host "Press any key to continue..."
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") > $null;
}
}
try {
$ExpiryDateTimeCast = [datetime]$ExpiryDateTime;
} catch {
Write-Error "ExpiryDateTime was not a parsable DateTime string. Including this parameter is a courtesy to the end-user so they're aware of when their WebHook will expire.";
AreYouSure;
exit;
}
if ( $ExpiryDateTimeCast -lt (Get-Date) ) {
Write-Error "It is likely this WebHook invocation will fail, as the expiry date for the webhook was $($ExpiryDateTimeCast -f "yyyyMMdd HHmm"). You will need to request/create a new one.";
AreYouSure;
} elseif ( $ExpiryDateTimeCast -lt (Get-Date).AddDays(30) ) {
Write-Warning "There is less than 30 days left of this WebHook's validity. It expires at $($ExpiryDateTimeCast -f "yyyyMMdd HHmm"). You may wish to request another."
AreYouSure;
}
$headers = @{"From"="$(whoami)";"Date"="$(Get-Date)"};
Write-Host "Invoking Webhook as $(whoami)..." -ForegroundColor Cyan -NoNewLine;
$response = Invoke-WebRequest -Method Post -Uri $uri -Headers $headers -Body $null;
$jobid = ConvertFrom-Json $response.Content;
if ( $response.StatusCode -eq 202 ) {
Write-Host "Success" -ForegroundColor Green;
Write-Host "`tResponse JSON: $($response.Content)";
} else {
Write-Host "Failed" -ForegroundColor Red;
$response | fl *;
AreYouSure;
}