AzureRestHelper.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
function Invoke-ArhRestMethod
{
    [CmdletBinding()]
    param
    (
        #Headers
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        $Headers,

        #Method
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet('Get','Patch')]
        [string]$Method,

        #Uri
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$Uri,

        #Body
        [Parameter(Mandatory=$false)]
        [object]$Body
    )

    process
    {
        $InvokeRestMethod_Params = @{
            Method=$Method
            Headers=$Headers
            Uri=$Uri
            UseBasicParsing=$true
        }
        if ($PSBoundParameters.ContainsKey('Body'))
        {
            $InvokeRestMethod_Params.Add('Body',$Body)
        }
        $restResult = Invoke-RestMethod @InvokeRestMethod_Params -ErrorAction Stop
        $restResult

        while ($restResult.nextLink)
        {
            $restResult = Invoke-RestMethod -Method 'Get' -Headers $Headers -Uri $restResult.nextLink -UseBasicParsing
            $restResult
        }
    }
}

function Get-ArhAuthorizationHeader
{
    [CmdletBinding()]
    param
    (
        #TenantId
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        $TenantId
    )

    process
    {
        $azProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
        $profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azProfile)
        $token = $profileClient.AcquireAccessToken($TenantId)            
        'Bearer ' + $token.AccessToken
    }
}