Development Tips
Tips for local development
Reusing Local Settings
### Read the local.settings.json file and convert to a PowerShell object.
$CIPPSettings = Get-Content .\local.settings.json | ConvertFrom-Json | Select-Object -ExpandProperty Values
### Loop through the settings and set environment variables for each.
$ValidKeys = @('TenantId', 'ApplicationId', 'ApplicationSecret', 'RefreshToken', 'ExchangeRefreshToken')
ForEach ($Key in $CIPPSettings.PSObject.Properties.Name) {
if ($ValidKeys -Contains $Key) {
[Environment]::SetEnvironmentVariable($Key, $CippSettings.$Key)
}
}### Setup body for the call to the Microsoft Graph API.
$AuthBody = @{
client_id = $ENV:ApplicationId
client_secret = $ENV:ApplicationSecret
scope = 'https://graph.microsoft.com/.default'
grant_type = 'refresh_token'
refresh_token = $ENV:RefreshToken
}
### Splat the parameters for the call to the Microsoft Graph API.
$AuthParams = @{
URI = "https://login.microsoftonline.com/$($ENV:TenantId)/oauth2/v2.0/token"
Body = $AuthBody
Method = 'POST'
ContentType = 'application/x-www-form-urlencoded'
ErrorAction = 'Stop'
}
### Make a call to the Microsoft Graph API for an access token.
$AccessToken = (Invoke-RestMethod @AuthParams).access_token
$GraphHeader = @{
Authorization = "Bearer $AccessToken"
}
### Splat the parameters for the call to the Microsoft Graph API.
$GraphParams = @{
URI = 'https://graph.microsoft.com/v1.0/contracts?$top=999'
Headers = $GraphHeader
Method = 'GET'
ErrorAction = 'Stop'
}
### Get all tenants your token has access to.
(Invoke-RestMethod @GraphParams).value | ftLast updated
Was this helpful?

