Created
December 6, 2022 21:03
-
-
Save aflyen/143c7684a551cf4df16c3d50398ac823 to your computer and use it in GitHub Desktop.
Send a POST reguest to Microsoft Graph from PowerShell
This file contains 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 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