Get-ExpiredWebhook.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
<#PSScriptInfo
 
.VERSION 1.0
 
.GUID af07f39d-34b6-4104-8121-69ebffdd9222
 
.AUTHOR Stefan Roth
 
.COMPANYNAME stefanroth.net
 
.LICENSEURI http://stefanroth.net
 
.DESCRIPTION
    This runbook gets all webhooks within in your resource group and checks if the webhook will expire within the defined time range (days). The runbook will check if the expiration date is within the date from now + days. If so it will send a mail out to the address specified. The runbook uses an Automation Credential to authenticate the user sending the email.
#>


<#
 
.SYNOPSIS
    Sends email about expiring webhooks
 
.PARAMETER To
    Set the email of the recipient e.g. max@domain.com
 
.PARAMETER From
    Set the email of the sending mail account e.g. admin@domain.com
 
.PARAMETER DaysToExpiration
    Provide within how many days from today the webhook will expire.
 
.PARAMETER AutomationCredentialAssetName
    Provide Azure Automation Asset name of the credential for accessing the Azure.
 
.PARAMETER MailCredentialAssetName
    Provide Azure Automation Asset name of the credential for accessing Office 365.
     
.EXAMPLE
    Get-ExpiredWebhook -To "stefan.roth@domain.ch" -From "admin@domain.net" -DaysToExpiration 200 -AutomationCredentialAssetName "Admin" -MailCredentialAssetName "Office365"
#>


[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)] [string] $To,
[Parameter(Mandatory=$True,Position=2)] [string] $From,
[Parameter(Mandatory=$True,Position=3)] [int] $DaysToExpiration,
[Parameter(Mandatory=$True,Position=4)] [string] $AutomationCredentialAssetName,
[Parameter(Mandatory=$True,Position=5)] [string] $MailCredentialAssetName
)
    #Update to the name of the credential asset in your Automation account
    # Get the credential asset with access to my Azure subscription
    $AzureCred = Get-AutomationPSCredential -Name $AutomationCredentialAssetName
    $MailCred = Get-AutomationPSCredential -Name $MailCredentialAssetName
    # Authenticate to Azure Service Management and Azure Resource Manager
    Add-AzureAccount -Credential $AzureCred | Out-Null
    Add-AzureRmAccount -Credential $AzureCred | Out-Null

    Get-AzureRmResourceGroup | `
        Get-AzureRmAutomationAccount | `
        Get-AzureRmAutomationWebhook | `
    ForEach($_ )`
        {  
            If ((New-TimeSpan -Start (Get-Date).ToUniversalTime() -End $_.ExpiryTime.UtcDateTime).Days -lt $DaysToExpiration)
            {  
                $body = @"
                Webhook "$($_.Name)" will expire on $($_.ExpiryTime).
                It is linked to $($_.ResourceGroupName)\$($_.AutomationAccountName)\$($_.RunbookName)
"@
       
                Write-Output $_
                Send-MailMessage -To $To -Subject "Webhook will expire" -Body $body -UseSsl -Port 587 -SmtpServer 'smtp.office365.com'  -From $From -BodyAsHtml -Credential $MailCred
            } 
        }