Microsoft365ServiceStatus.psm1

#Region '.\_PrefixCode.ps1' 0
# Code in here will be prepended to top of the psm1-file.
#EndRegion '.\_PrefixCode.ps1' 1
#Region '.\Private\Get-APIUrl.ps1' 0

function Get-APIUrl {
    [CmdletBinding()]
    param (
        
    ) 

    process {
        return @{
            URL = $script:apiURL
        }   
    }
}
#EndRegion '.\Private\Get-APIUrl.ps1' 13
#Region '.\Private\Get-AuthHeader.ps1' 0
<#
.SYNOPSIS
    Builds authorization header
.DESCRIPTION
    Builds and authorization header containing the stored MSAL access token which is passed within the "Header" section for Invoke-RestMethod calls
.EXAMPLE
    Get-AuthHeader
    Returns authorization header
#>

function Get-AuthHeader {
    [CmdletBinding()]
    param (

    )

    process {
        if ($null -ne $script:msalToken) {
            return @{
                "Content-Type"  = "application/json"
                "Authorization" = $script:msalToken.CreateAuthorizationHeader()
                "ExpiresOn"     = $script:msalToken.ExpiresOn.LocalDateTime
            }
        }
        else {
            throw "You need to obtain and store an access token with `"Get-MsalToken`" & `"Set-MsalToken`" before you can use this module."
        }
    }
}
#EndRegion '.\Private\Get-AuthHeader.ps1' 28
#Region '.\Public\Get-M365ServiceHistoricalStatus.ps1' 0
<#
.SYNOPSIS
    Retrieves historical Microsoft 365 Service Status information
.DESCRIPTION
    Retrieves historical service status from the Office 365 Service Communications API
.EXAMPLE
    Get-M365ServiceHistoricalStatus
    Lists historical service status
#>

function Get-M365ServiceHistoricalStatus {
    [CmdletBinding()]
    param (
        
    )
    
    process {
        try {
            $uri = "$script:apiURL/HistoricalStatus"
            $historicalStatus = Invoke-RestMethod -Method Get -Uri $uri -Headers $(Get-AuthHeader) -UseBasicParsing -EA Stop
            return $historicalStatus.value
        }
        catch {
            Write-Error $_
        }
    }
}
#EndRegion '.\Public\Get-M365ServiceHistoricalStatus.ps1' 26
#Region '.\Public\Get-M365ServiceMessage.ps1' 0
<#
.SYNOPSIS
    Retrieves Microsoft 365 Service Status Messages
.DESCRIPTION
    Retrieves service status messages from the Office 365 Service Communications API
.EXAMPLE
    Get-M365ServiceMessage
    Lists 100 most recent service messages within the tenant including status information
.EXAMPLE
    Get-M365ServiceMessage -MessageID EX213379
    Fetches specific service status message
#>

function Get-M365ServiceMessage {
    [CmdletBinding()]
    param (
        # Filter by Message ID
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]
        $MessageID
    )

    process {
        try {

            $filter = $null

            if ($MessageID){
                $filter = "?`$filter=Id eq '$MessageID'"
            }

            $uri = "$script:apiURL/Messages" + $filter
            $serviceMessages = Invoke-RestMethod -Method Get -Uri $uri -Headers $(Get-AuthHeader) -UseBasicParsing -EA Stop
            return $serviceMessages.value
        }
        catch {
            Write-Error $_
        }
    }
}
#EndRegion '.\Public\Get-M365ServiceMessage.ps1' 40
#Region '.\Public\Get-M365ServiceStatus.ps1' 0
<#
.SYNOPSIS
    Retrieves Microsoft 365 Service Status
.DESCRIPTION
    Retrieves service status from the Office 365 Service Communications API
.EXAMPLE
    Get-M365ServiceStatus
    Lists all available services within the tenant including status information
.EXAMPLE
    Get-M365ServiceStatus -Brief
    Lists all available services within the tenant including most important status information
#>

function Get-M365ServiceStatus {
    [CmdletBinding()]
    param (
        # Only show brief status
        [Parameter()]
        [switch]
        $Brief
    )

    process {
        try {
            $uri = "$script:apiURL/CurrentStatus"
            $serviceStatus = Invoke-RestMethod -Method Get -Uri $uri -Headers $(Get-AuthHeader) -UseBasicParsing -EA Stop

            if ($Brief.isPresent) {
                return $serviceStatus.value | Select-Object -Property WorkloadDisplayName, StatusDisplayName, IncidentIds, StatusTime
            }
            else {
                return $serviceStatus.value
            }  
        }
        catch {
            Write-Error $_
        }
    }
}
#EndRegion '.\Public\Get-M365ServiceStatus.ps1' 38
#Region '.\Public\Set-MsalToken.ps1' 0
<#
.SYNOPSIS
    Stores a MSAL token for future usage with this module.
.DESCRIPTION
    This command will store an MSAL token within the $script: scope.
.EXAMPLE
    $accessToken = Get-MsalToken -ClientId "10d3c3cb-8b79-481c-bf48-1c35eceb0c75" -TenantId "nicolasuter.ch" -Scope "https://manage.office.com/ServiceHealth.Read"
    Set-MsalToken -MsalToken $accessToken
    Stores the MSAL access token for this module to call other cmdlets.
#>

function Set-MsalToken {
    [CmdletBinding(SupportsShouldProcess)]
    param (
        # Store MsalToken
        [Parameter(Mandatory)]
        [object]
        $MsalToken
    )

    process {
        if ($PSCmdlet.ShouldProcess($script:msalToken, "Storing token for this module")) {
            $script:msalToken = $MsalToken
            $script:apiURL = "https://manage.office.com/api/v1.0/$($script:msalToken.TenantId)/ServiceComms"
        }
    }
}
#EndRegion '.\Public\Set-MsalToken.ps1' 26