Skip to content

PowerShell module Azure REST API call handler for ARM, Microsoft Graph, KeyVault, LogAnalytics

Notifications You must be signed in to change notification settings

BrooksV/AzAPICall

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AzAPICall

PowerShell Gallery Version (including pre-releases)

You want to have an easy way to interact with the Microsoft Azure API endpoints without getting headache of taking care of valid bearer token and error handling?

Table of content

AzAPICall example

Get & Set AzAPICall PowerShell module

Install-Module -Name AzAPICall
#Import-Module -Name AzAPICall

Connect to Azure

Connect-AzAccount

Initialize AzAPICall

$parameters4AzAPICallModule = @{
    #SubscriptionId4AzContext = $null #specify Subscription Id
    #DebugAzAPICall = $true
    #writeMethod = 'Output' #Debug, Error, Host, Information, Output, Progress, Verbose, Warning (default: host)
    #debugWriteMethod = 'Warning' #Debug, Error, Host, Information, Output, Progress, Verbose, Warning (default: host)
}
$azAPICallConf = initAzAPICall @parameters4AzAPICallModule

Use AzAPICall

AzAPICall -uri "$($azAPICallConf['azAPIEndpointUrls'].MicrosoftGraph)/v1.0/groups" -AzAPICallConfiguration $azAPICallConf

AzAPICallExample.ps1

Public functions

  • AzAPICall
  • initAzAPICall
  • getAzAPICallFunctions
  • getAzAPICallRuleSet
  • createBearerToken

createBearerToken example:

$azAPICallConf = initAzAPICall
createBearerToken -AzAPICallConfiguration $azapicallconf -targetEndPoint 'Storage'
$azAPICallConf['htBearerAccessToken'].Storage

Supported endpoints

Endpoint Endpoint URL (AzureCloud) Variable
Microsoft Graph https://graph.microsoft.com $azAPICallConf['azAPIEndpointUrls'].MicrosoftGraph
ARM (Azure Resource Management) https://management.azure.com $azAPICallConf['azAPIEndpointUrls'].ARM
Azure Key Vault https://vault.azure.net $azAPICallConf['azAPIEndpointUrls'].KeyVault
Log Analytics https://api.loganalytics.io/v1 $azAPICallConf['azAPIEndpointUrls'].LogAnalytics
Storage (blob) https://<storageAccountName>.blob.core.windows.net https://storageAccountName.blob.$($azAPICallConf['azAPIEndpointUrls'].Storage)

Add a new endpoint -> setAzureEnvironment.ps1

AzAPICall Parameters

Field Type Description Required
uri string $azAPICallConf['azAPIEndpointUrls'].MicrosoftGraph)/v1.0/groups which translates to: https://graph.microsoft.com/v1.0/groups
AzAPICallConfiguration object Set of prebuilt ($azAPICallConf = initAzAPICall) variables required for AzAPICall operations (-AzAPICallConfiguration $azAPICallConf)
method string Method for the API request (e.g. GET, POST, ..) default is 'GET', else define it
currentTask string Free text field; in case of error or enabled -DebugAzAPICall currentTask will be output to console
body string Request Body for the API request - Example
caller string Set the value to CustomDataCollection for parallelization to have different font colors for the debug output
consistencyLevel string For several OData query parameters the consistencyLevel-header need to be set to eventual
listenOn string Default is Value. Depending to the expected response of the API call the following values are accepted: Content, ContentProperties or StatusCode
noPaging switch If value is true paging will be deactivated and you will only get the defined number of $top results or Resource Graph limits any query to returning only 100 records. Otherwise, you can use $top to increase the result batches from default 100 up to 999 for the AzAPICall. Value for $top must range from 1 to 999
validateAccess switch Use this parameter if you only want to validate that the requester has permissions to the enpoint, if authorization is denied AzAPICall returns 'failed'. (Using -validateAccess will set noPaging to true)
skipOnErrorCode int32 In some cases (e.g. trying to add a user to a group were the user is already a member of) the API responde with an http status code 400. This is an expected error. To not throw an error and exit the script, you can use this parameter and set an expected error status code like 400. (example: .error.message: 'One or more added object references already exist for the following modified properties: 'members'.')

Good to know

By default, endPoints return results in batches of e.g. 100. You can increase the return count defining e.g. $top=999 ($top requires use of consistencyLevel = eventual)

General Parameters

Field Type Description Required
DebugAzAPICall bool Set to true to enable debug output
SubscriptionId4AzContext string Specify if specific subscription should be used for the AzContext (Subscription Id / GUID)
writeMethod string Write method. Debug, Error, Host, Information, Output, Progress, Verbose, Warning (default: host)
debugWriteMethod string Write method in case of wanted or enforced debug. Debug, Error, Host, Information, Output, Progress, Verbose, Warning (default: host)
AzAPICallCustomRuleSet object wip

AzAPICall Tracking

To get some insights on all API calls you can check the $azAPICallConf['arrayAPICallTracking'] object (synchronized ArrayList)

$azAPICallConf['arrayAPICallTracking'][0] | ConvertTo-Json
{
  "CurrentTask": "Microsoft Graph API: Get - Groups",
  "TargetEndpoint": "MicrosoftGraph",
  "Uri": "https://graph.microsoft.com/v1.0/groups?$top=999&$filter=(mailEnabled eq false and securityEnabled eq true)&$select=id,createdDateTime,displayName,description&$orderby=displayName asc&$count=true",
  "Method": "GET",
  "TryCounter": 0,
  "TryCounterUnexpectedError": 0,
  "RetryAuthorizationFailedCounter": 0,
  "RestartDueToDuplicateNextlinkCounter": 0,
  "TimeStamp": "2022011316040343",
  "Duration": 1.3137266
}

As well you can see how fast a AzAPICall was responding:

($azAPICallConf['arrayAPICallTracking'].Duration | Measure-Object -Average -Maximum -Minimum) | ConvertTo-Json
{
  "Count": 1000,
  "Average": 0.4292551101999999,
  "Sum": null,
  "Maximum": 2.7991866,
  "Minimum": 0.263543,
  "StandardDeviation": null,
  "Property": null
}

Prerequisites

Powershell Modules

PowerShell Module
Az.Accounts

Contribute

Your contribution is welcome.

Thanks to the awesome contributors:

  • Brooks Vaugn
  • Kai Schulz
  • Simon Wahlin
  • Tim Stock
  • Tim Wanierke

About

PowerShell module Azure REST API call handler for ARM, Microsoft Graph, KeyVault, LogAnalytics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PowerShell 99.8%
  • Dockerfile 0.2%