functions/Private/Converting/Build-MgaUri.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
function Build-MgaUri {
    param (
        $Uri,
        $Api
    )
    try {
        $Verbose = 'Formatted Uri to: ' 
        if ($Uri -like 'https://graph.microsoft.com/*') {
            $Uri = $Uri
        }
        else {
            if ($Uri -like '/v1.0/*') {
                $Uri = "https://graph.microsoft.com$Uri"
            }
            elseif ($Uri -like 'v1.0/*') {
                $Uri = "https://graph.microsoft.com/$Uri"
            }
            elseif ($Uri -like '/beta/*') {
                $Uri = "https://graph.microsoft.com$Uri"
            }
            elseif ($Uri -like 'beta/*') {
                $Uri = "https://graph.microsoft.com/$Uri"
            }
            elseif ($Uri -like '/*') {
                $Uri = "https://graph.microsoft.com/v1.0$Uri"
            }
            else {
                $Uri = "https://graph.microsoft.com/v1.0/$Uri"
            }
        }
        if ($Api -eq 'beta') {
            $Uri = $Uri -Replace '/v1.0/', '/beta/'
        }
        elseif ($Api -eq 'v1.0') {
            $Uri = $Uri -Replace '/beta/', '/v1.0/'
        }
        Write-Verbose "$Verbose$Uri"
        return $Uri
    }
    catch {
        throw $_
    }
}