Skip to content

Instantly share code, notes, and snippets.

@aflyen
Created December 6, 2022 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aflyen/143c7684a551cf4df16c3d50398ac823 to your computer and use it in GitHub Desktop.
Save aflyen/143c7684a551cf4df16c3d50398ac823 to your computer and use it in GitHub Desktop.
Send a POST reguest to Microsoft Graph from PowerShell
function Send-CorpMicrosoftGraphPostRequest
{
Param(
[Parameter(Mandatory=$true)]
[string]$Domain,
[Parameter(Mandatory=$true)]
[string]$AppId,
[Parameter(Mandatory=$true)]
[string]$AppSecret,
[Parameter(Mandatory=$true)]
[string]$RequestUrl,
[Parameter(Mandatory=$false)]
[string]$RequestBody
)
$Result = $null
try
{
# Connect to Microsoft Graph to aqure a valid token
$Token = Get-CorpMicrosoftGraphAccessToken -TenantId $Domain -ClientId $AppId -ClientSecret $AppSecret
# Request
$Headers = @{"Content-Type" = "application/json" ; "Authorization" = "Bearer " + $Token}
# Send request
if ($RequestBody -eq $null)
{
$Result = Invoke-RestMethod -Method Post -Uri $RequestUrl -Headers $Headers
}
else
{
$Result = Invoke-RestMethod -Method Post -Uri $RequestUrl -Headers $Headers -Body $RequestBody
}
}
catch
{
$Result = $_.Exception.Response.StatusCode.value__
}
return $Result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment