source/public/Get-MS365Messages.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
# Function to get Office 365 Messages
Function Get-MS365Messages {
    [cmdletbinding()]
    param(
        [parameter(Mandatory, Position = 0)]
        [ValidateNotNullOrEmpty()]
        [string]
        $Token,

        [parameter()]
        [ValidateNotNullOrEmpty()]
        [string[]]$Workload,

        [parameter()]
        [datetime]$LastUpdatedTime,

        [parameter()]
        [ValidateSet('Ongoing', 'Resolved')]
        [string]$Status
    )

    $header = @{'Authorization' = "Bearer $($Token)" }
    # $uri = "https://manage.office.com/api/v1.0/$($tenantID)/ServiceComms/Messages"
    $uri = "https://graph.microsoft.com/beta/admin/serviceAnnouncement/issues"

    if ($Status -eq 'Ongoing') {
        $uri = "$uri`?`$filter=isResolved ne true"
    }

    if ($Status -eq 'Resolved') {
        $uri = "$uri`?`$filter=isResolved eq true"
    }

    if ($LastUpdatedTime) {
        $lastModifiedDateTime = Get-Date ($LastUpdatedTime.ToUniversalTime()) -UFormat "%Y-%m-%dT%RZ"
        $uri = "$uri and lastModifiedDateTime ge $lastModifiedDateTime"
    }

    if ($Workload) {
        $uri = "$uri and (service eq '$($Workload[0])'"
        for ($i = 1; $i -lt $workload.Count; $i++) {
            $uri = "$uri or service eq '$($Workload[$i])'"
        }
        $uri = "$uri)"
    }

    SayInfo "Query = $uri"

    try {
        $result = @((Invoke-RestMethod -Uri $uri -Headers $header -Method Get -ErrorAction Stop).value)
        return $result
    }
    catch {
        SayError "$($_.Exception.Message)"
        return $null
    }
}