ReportHTMLHelpers.psm1

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
Function Get-HostUptime 
{
    param ([string]$ComputerName)
    $Uptime = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName
    $LastBootUpTime = $Uptime.ConvertToDateTime($Uptime.LastBootUpTime)
    $Time = (Get-Date) - $LastBootUpTime
    Return '{0:00} Days, {1:00} Hours, {2:00} Minutes, {3:00} Seconds' -f $Time.Days, $Time.Hours, $Time.Minutes, $Time.Seconds
}

Function Test-AzureRMAccountTokenExpiry
{
    # Credit James Rooke    
    [CmdletBinding()]
    Param 
    (
        
    )
    
    $TokenExpiredOrDoesNotExist = $false
    try
    {
        $TokenCache = [Microsoft.IdentityModel.Clients.ActiveDirectory.TokenCache]::DefaultShared
        $ContextTenantId = [Microsoft.WindowsAzure.Commands.Common.AzureRMProfileProvider]::Instance.Profile.Context.Tenant.Id.Guid
        $TenantToken = $TokenCache.ReadItems() | Where-Object { $_.TenantId -eq $ContextTenantId }
        $CurrentDateTime = Get-Date
        Write-Verbose "Context Tenant Token ExpiresOn: $($TenantToken.ExpiresOn)"
        Write-Verbose "Current DateTime in UTC: $($CurrentDateTime.ToUniversalTime())"
        if ($TenantToken.ExpiresOn -lt $CurrentDateTime)
        {
            Write-Verbose "Tenant Token has expired, calling Add-AzureRmAccount"
            $TokenExpiredOrDoesNotExist = $true
        }
        else
        {
            Write-Verbose "Tenant Token is still valid"
        }
    }
    catch [System.Management.Automation.RuntimeException]
    {
        $TokenExpiredOrDoesNotExist = $true
    }
 
    if ($TokenExpiredorDoesNotExist) { 
        try 
        {
            Add-AzureRmAccount
        }
        catch
        {
            Write-Warning "Error"
            break
        }
        Finally
        {
        
        }
    }
}

Function Connect-AzureRunAsConnection

{
    #Credit Keith Ellis
    [CmdletBinding()]
    Param 
    (
        
    )
    Write-Output ("Prepare Azure Connection")
    $connectionName = "AzureRunAsConnection"
    try
    {
        # Get the connection "AzureRunAsConnection "
        $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         

        #"Logging in to Azure..."
        Add-AzureRmAccount `
            -ServicePrincipal `
            -TenantId $servicePrincipalConnection.TenantId `
            -ApplicationId $servicePrincipalConnection.ApplicationId `
            -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint | Out-Null 
    }
    catch {
        if (!$servicePrincipalConnection)
        {
            $ErrorMessage = "Connection $connectionName not found."
            throw $ErrorMessage
        } else{
            Write-Error -Message $_.Exception
            throw $_.Exception
        }
    }
}