PSWinDocumentation.O365HealthService.psm1

function Connect-O365ServiceHealth {
    param(
        [string][alias('ClientID')] $ApplicationID,
        [string][alias('ClientSecret')] $ApplicationKey,
        [string] $TenantDomain
    )
    $Body = @{
        grant_type    = "client_credentials"
        resource      = "https://manage.office.com"
        client_id     = $ApplicationID
        client_secret = $ApplicationKey
    }

    $Authorization = Invoke-RestMethod -Method Post -Uri "https://login.microsoftonline.com/$($TenantDomain)/oauth2/token?api-version=1.0" -Body $body

    if ($Authorization) {
        @{'Authorization' = "$($Authorization.token_type) $($Authorization.access_token)"}
    } else {
        $null
    }
}
function Get-Office365ServiceHealthCurrentStatus {
    param(
        [System.Collections.IDictionary] $Authorization,
        [string] $TenantDomain
    )
    $CurrentStatus = (Invoke-RestMethod -Uri "https://manage.office.com/api/v1.0/$($TenantDomain)/ServiceComms/CurrentStatus" -Headers $Authorization -Method Get)

    $Output = @{}
    $Output.Simple = foreach ($Status in $CurrentStatus.Value) {
        [PSCustomObject] @{
            ID                  = $Status.ID
            IncidentIds         = $Status.IncidentIds -join ', '
            Status              = $Status.Status
            StatusDisplayName   = $Status.StatusDisplayName
            StatusTime          = $Status.StatusTime
            Workload            = $Status.Workload
            WorkloadDisplayName = $Status.WorkloadDisplayName
        }
    }

    $Output.Exteneded = foreach ($Status in $CurrentStatus.Value) {
        foreach ($Feature in  $Status.FeatureStatus) {
            [PSCustomObject] @{
                ID                              = $Status.ID
                IncidentIds                     = $Status.IncidentIds -join ', '
                Status                          = $Status.Status
                StatusDisplayName               = $Status.StatusDisplayName
                StatusTime                      = $Status.StatusTime
                Workload                        = $Status.Workload
                WorkloadDisplayName             = $Status.WorkloadDisplayName
                FeatureDisplayName              = $Feature.FeatureDisplayName
                FeatureName                     = $Feature.FeatureName
                FeatureServiceStatus            = $Feature.FeatureServiceStatus
                FeatureServiceStatusDisplayName = $Feature.FeatureServiceStatusDisplayName
            }
        }
    }
    return $Output
}
function Get-Office365ServiceHealthHistoricalStatus {
    [CmdLetbinding()]
    param(
        [System.Collections.IDictionary] $Authorization,
        [string] $TenantDomain
    )
    $HistoricalStatus = (Invoke-RestMethod -Uri "https://manage.office.com/api/v1.0/$($TenantDomain)/ServiceComms/HistoricalStatus" -Headers $Authorization -Method Get)

    $Output = @{}
    $Output.Simple = foreach ($Status in $HistoricalStatus.Value) {
        [PSCustomObject] @{
            ID                  = $Status.ID
            IncidentIds         = $Status.IncidentIds -join ', '
            Status              = $Status.Status
            StatusDisplayName   = $Status.StatusDisplayName
            StatusTime          = $Status.StatusTime
            Workload            = $Status.Workload
            WorkloadDisplayName = $Status.WorkloadDisplayName
        }
    }
    $Output.Exteneded = foreach ($Status in $HistoricalStatus.Value) {
        foreach ($Feature in  $Status.FeatureStatus) {
            [PSCustomObject] @{
                ID                              = $Status.ID
                IncidentIds                     = $Status.IncidentIds -join ', '
                Status                          = $Status.Status
                StatusDisplayName               = $Status.StatusDisplayName
                StatusTime                      = $Status.StatusTime
                Workload                        = $Status.Workload
                WorkloadDisplayName             = $Status.WorkloadDisplayName
                FeatureDisplayName              = $Feature.FeatureDisplayName
                FeatureName                     = $Feature.FeatureName
                FeatureServiceStatus            = $Feature.FeatureServiceStatus
                FeatureServiceStatusDisplayName = $Feature.FeatureServiceStatusDisplayName
            }
        }
    }
    return $Output
}
function Get-Office365ServiceHealthMessages {
    param(
        [System.Collections.IDictionary] $Authorization,
        [string] $TenantDomain
    )
    $Messages = (Invoke-RestMethod -Uri "https://manage.office.com/api/v1.0/$($TenantDomain)/ServiceComms/Messages" -Headers $Authorization -Method Get)

    $Output = @{}
    $Simple = foreach ($Message in $Test.Value) {
        $Messages = $Message.Messages
        [PSCustomObject] @{
            Id                           = $Message.Id
            Title                        = $Message.Title
            ImpactDescription            = $Message.ImpactDescription
            LastUpdatedTime              = $Message.LastUpdatedTime
            MessageType                  = $Message.MessageType
            Status                       = $Message.Status
            Severity                     = $Message.Severity
            StartTime                    = $Message.StartTime


            Workload                     = $Message.Workload
            WorkloadDisplayName          = $Message.WorkloadDisplayName
            ActionType                   = $Message.ActionType
            Classification               = $Message.Classification
            EndTime                      = $Message.EndTime
            Feature                      = $Message.Feature
            FeatureDisplayName           = $Message.FeatureDisplayName
            UserFunctionalImpact         = $Message.UserFunctionalImpact
            PostIncidentDocumentUrl      = $Message.PostIncidentDocumentUrl
            AffectedTenantCount          = $Message.AffectedTenantCount
            AffectedUserCount            = $Message.AffectedUserCount
            AffectedWorkloadDisplayNames = $Message.AffectedWorkloadDisplayNames -join ','
            AffectedWorkloadNames        = $Message.AffectedWorkloadNames -join ','
        }
    }

    $Exteneded = foreach ($Message in $Test.Value) {
        $Messages = $Message.Messages
        foreach ($M in $Messages) {
            [PSCustomObject] @{
                Id                           = $Message.Id
                Title                        = $Message.Title
                ImpactDescription            = $Message.ImpactDescription
                LastUpdatedTime              = $Message.LastUpdatedTime
                MessageType                  = $Message.MessageType
                Status                       = $Message.Status
                Severity                     = $Message.Severity
                StartTime                    = $Message.StartTime
                Message                      = $M.MessageText

                Workload                     = $Message.Workload
                WorkloadDisplayName          = $Message.WorkloadDisplayName
                ActionType                   = $Message.ActionType
                Classification               = $Message.Classification
                EndTime                      = $Message.EndTime
                Feature                      = $Message.Feature
                FeatureDisplayName           = $Message.FeatureDisplayName
                UserFunctionalImpact         = $Message.UserFunctionalImpact
                PostIncidentDocumentUrl      = $Message.PostIncidentDocumentUrl
                AffectedTenantCount          = $Message.AffectedTenantCount
                AffectedUserCount            = $Message.AffectedUserCount
                AffectedWorkloadDisplayNames = $Message.AffectedWorkloadDisplayNames -join ','
                AffectedWorkloadNames        = $Message.AffectedWorkloadNames -join ','
            }
        }
    }

    $Output.MessageCenterInformationSimple = $Simple | & { process { if ($_.MessageType -eq 'MessageCenter' ) { $_ } } }
    $Output.IncidentsSimple = $Simple | & { process { if ($_.MessageType -eq 'Incident' ) { $_ } } }

    $Output.MessageCenterInformation = $Exteneded | & { process { if ($_.MessageType -eq 'MessageCenter' ) { $_ } } }
    $Output.Incidents = $Exteneded | & { process { if ($_.MessageType -eq 'Incident' ) { $_ } } }
    return $Output
}
function Get-Office365ServiceHealthServices {
    param(
        [System.Collections.IDictionary] $Authorization,
        [string] $TenantDomain
    )
    $Services = (Invoke-RestMethod -Uri "https://manage.office.com/api/v1.0/$($TenantDomain)/ServiceComms/Services" -Headers $Authorization -Method Get)

    $Output = @{}
    $Output.Simple = foreach ($Service in $Services.Value) {
        [PSCustomObject] @{
            ID          = $Service.ID
            DisplayName = $Service.DisplayName
        }
    }

    $Output.Exteneded = foreach ($Service in $Services.Value) {
        foreach ($Feature in  $Service.Features) {
            [PSCustomObject] @{
                ID                 = $Service.ID
                DisplayName        = $Service.DisplayName
                FeatureDisplayName = $Feature.DisplayName
                FeatureName        = $Feature.Name
            }
        }
    }
    return $Output
}
function Get-Office365ServiceHealth {
    [CmdLetbinding()]
    param(
        [string][alias('ClientID')] $ApplicationID,
        [string][alias('ClientSecret')] $ApplicationKey,
        [string] $TenantDomain
    )

    $Authorization = Connect-O365ServiceHealth -ApplicationID $ApplicationID -ApplicationKey $ApplicationKey -TenantDomain $TenantDomain
    if ($null -ne $Authorization) {
        $Services = Get-Office365ServiceHealthServices -Authorization $Authorization -TenantDomain $TenantDomain
        $CurrentStatus = Get-Office365ServiceHealthCurrentStatus -Authorization $Authorization -TenantDomain $TenantDomain
        $HistoricalStatus = Get-Office365ServiceHealthHistoricalStatus -Authorization $Authorization -TenantDomain $TenantDomain
        $Messages = Get-Office365ServiceHealthMessages -Authorization $Authorization -TenantDomain $TenantDomain
    }
    $Output = [ordered] @{}
    $Output.ServicesSimple = $Services.Simple
    $Output.ServicesExteneded = $Services.Exteneded
    $Output.CurrentStatusSimple = $CurrentStatus.Simple
    $Output.CurrentStatusExteneded = $CurrentStatus.Exteneded
    $Output.HistoricalStatusSimple = $HistoricalStatus.Simple
    $Output.HistoricalStatusExteneded = $HistoricalStatus.Exteneded
    $Output.MessageCenterInformationSimple = $Messages.MessageCenterInformationSimple | Sort-Object -Property LastUpdatedTime -Descending
    $Output.MessageCenterInformation = $Messages.MessageCenterInformation | Sort-Object -Property LastUpdatedTime -Descending
    $Output.IncidentsSimple = $Messages.IncidentsSimple | Sort-Object -Property LastUpdatedTime -Descending
    $Output.Incidents = $Messages.Incidents | Sort-Object -Property LastUpdatedTime -Descending
    return $Output
}



Export-ModuleMember `
    -Function @('Get-Office365ServiceHealth') `
    -Alias @()