Created
February 5, 2022 23:38
-
-
Save M1kep/42ed410214dbcfa064e4267df063d590 to your computer and use it in GitHub Desktop.
Function for making a request to the Az Portal API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Invoke-AzPortalRequest { | |
<# | |
.SYNOPSIS | |
Runs a command against the Azure Portal API | |
.NOTES | |
Originally stolen from https://github.com/JustinGrote/Az.PortalAPI/blob/master/Az.PortalAPI/Public/Invoke-Request.ps1 | |
#> | |
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')] | |
param ( | |
#The target of your request. This is appended to the Portal API URI. Example: Permissions | |
[Parameter(Mandatory)] | |
[String]$Target, | |
#The command you wish to execute. Example: GetUserSystemRoleTemplateIds | |
[Parameter()] | |
[String]$Action, | |
#The body of your request. This is usually in JSON format | |
[Parameter()] | |
[Object]$Body, | |
[Parameter()] | |
#Specify the HTTP Method you wish to use. Defaults to GET | |
[ValidateSet("GET", "POST", "OPTIONS", "DELETE")] | |
[String]$Method = "GET", | |
[Parameter()] | |
[String]$ContentType = 'application/json', | |
#Your Azure Context. This will be discovered automatically if you have already logged in with Connect-AzAccount | |
[Microsoft.Azure.Commands.Profile.Models.Core.PSAzureContext] | |
$Context = (Get-AzContext | Select-Object -first 1), | |
#Your Access token. By default this is discovered from your Azure Context. | |
[Parameter()] | |
$apiToken = (Get-AzPortalToken), | |
#The base URI for the Portal API. Typically you don't need to change this | |
[Parameter()] | |
[Uri]$baseURI = 'https://main.iam.ad.ext.azure.com/api/', | |
[Parameter()] | |
[URI]$requestOrigin = 'https://iam.hosting.portal.azure.net', | |
#The request ID for the session. You can generate one with [guid]::NewGuid().guid. | |
#Typically you only specify this if you're trying to retry an operation and don't want to duplicate the request, such as for a POST operation | |
[Parameter()] | |
[guid]$requestID = [guid]::NewGuid().guid | |
) | |
#Combine the BaseURI and Target | |
[String]$ApiAction = $Target | |
if ($Action) { | |
$ApiAction = $ApiAction + '/' + $Action | |
} | |
$InvokeRestMethodParams = @{ | |
Uri = [Uri]::New($baseURI, $ApiAction) | |
Method = $Method | |
Header = [ordered]@{ | |
Authorization = 'Bearer ' + $apiToken.AccessToken.tostring() | |
'Content-Type' = $ContentType | |
'x-ms-client-request-id' = $requestID | |
'Host' = $baseURI.Host | |
'Origin' = 'https://iam.hosting.portal.azure.net' | |
} | |
Body = $Body | |
} | |
#Only care about Whatif for POST and DELETE. Other commands don't change data | |
if ($Method -match "POST|DELETE") { | |
$shouldProcessMessage = $METHOD | |
if ($action) { $shouldProcessMessage = $shouldProcessMessage, $action -join ' ' } | |
if ($body) { $shouldProcessMessage = $shouldProcessMessage, $body -join ': ' } | |
if ($PSCmdlet.ShouldProcess($target, $shouldProcessMessage)) { | |
Invoke-RestMethod @InvokeRestMethodParams | |
} | |
} | |
else { | |
Invoke-RestMethod @InvokeRestMethodParams | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment