Functions/Public/MSGraph.ps1

Function Get-MSAzureGraphSubscriptions {
    <#
        .SYNOPSIS
        Return data about the subscriptions for a given Azure tenant.
         
        .DESCRIPTION
        Return data about the subscriptions for a given Azure tenant.
         
        .PARAMETER MSClientID
        The MS client ID for the application granted access to Azure AD.
         
        .PARAMETER MSClientSecret
        The MS client secret for the application granted access to Azure AD.
         
        .PARAMETER MSTenantID
        The MS tenant ID for the O365 customer granted access to Azure AD.
         
        .PARAMETER TenantName
        The name of the Nectar DXP tenant. Used in multi-tenant configurations.
         
        .PARAMETER AuthToken
        The authorization token used for this request. Normally obtained via Get-MSGraphAccessToken
 
        .EXAMPLE
        Get-MSAzureGraphSubscriptions
 
        .NOTES
        Version 1.0
    #>

    
    Param (
        [Parameter(ValueFromPipelineByPropertyName, Mandatory=$False)]
        [string]$MSClientID,
        [Parameter(ValueFromPipelineByPropertyName, Mandatory=$False)]
        [string]$MSClientSecret,
        [Parameter(ValueFromPipelineByPropertyName, Mandatory=$False)]
        [string]$MSTenantID,
        [Parameter(ValueFromPipelineByPropertyName, Mandatory=$False)]
        [string]$TenantName,
        [Parameter(ValueFromPipelineByPropertyName, Mandatory=$False)]
        [string]$AuthToken    
    )
    
    Process {
        Try {
            If ($MSTenantID) { 
                $AuthToken = Get-MSGraphAccessToken -MSClientID $MSClientID -MSClientSecret $MSClientSecret -MSTenantID $MSTenantID
            }
            ElseIf (!$AuthToken) {
                $AuthToken = Get-NectarMSTeamsConfig -TenantName $TenantName | Get-MSGraphAccessToken
            }

            # Test Azure AD access
            $Headers = @{
                Authorization = "Bearer $AuthToken"
            }
            
            $URI = "https://graph.microsoft.com/v1.0/subscriptions"
            Write-Verbose $URI
            $JSON = Invoke-RestMethod -Method GET -URI $URI -Headers $Headers
            Return $JSON.value
        }
        Catch {
            Write-Error "Could not obtain subscription information. $($_.Exception.Message)"
            If ($PSCmdlet.MyInvocation.BoundParameters["ErrorAction"] -ne "SilentlyContinue") { Get-JSONErrorStream -JSONResponse $_ }
        }
    }
}