Skip to content

Instantly share code, notes, and snippets.

@sandytsang
Last active October 25, 2020 05:44
Show Gist options
  • Select an option

  • Save sandytsang/e69ff8ab646198b3f9e4b0c08e6d48fb to your computer and use it in GitHub Desktop.

Select an option

Save sandytsang/e69ff8ab646198b3f9e4b0c08e6d48fb to your computer and use it in GitHub Desktop.
# Check if the Azure AD PowerShell module has already been loaded.
if ( ! ( Get-Module AzureAD ) ) {
# Check if the Azure AD PowerShell module is installed.
if ( Get-Module -ListAvailable -Name AzureAD ) {
# The Azure AD PowerShell module is not load and it is installed. This module must be loaded for other operations performed by this script.
Write-Host -ForegroundColor Green "Loading the Azure AD PowerShell module..."
Import-Module AzureAD
} else {
Install-Module AzureAD
}
}
try {
Write-Host -ForegroundColor Green "When prompted please enter the appropriate credentials..."
Connect-AzureAD
} catch [Microsoft.Azure.Common.Authentication.AadAuthenticationCanceledException] {
# The authentication attempt was canceled by the end-user. Execution of the script should be halted.
Write-Host -ForegroundColor Yellow "The authentication attempt was canceled."
Exit
} catch {
# An unexpected error has occurred. The end-user should be notified so that the appropriate action can be taken.
Write-Error "An unexpected error has occurred. Please review the following error message and try again." `
"$($Error[0].Exception)"
}
#You can choose your own application name here
$DisplayName = "Intune Graph App auth"
$AppReplyURI = "https://localhost"
$graphAppAccess = [Microsoft.Open.AzureAD.Model.RequiredResourceAccess]@{
ResourceAppId = "00000003-0000-0000-c000-000000000000";
ResourceAccess =
[Microsoft.Open.AzureAD.Model.ResourceAccess]@{
Id = "06a5fe6d-c49d-46a7-b082-56b1b14103c7";
Type = "Role"},
[Microsoft.Open.AzureAD.Model.ResourceAccess]@{
Id = "5ac13192-7ace-4fcf-b828-1a26f28068ee";
Type = "Role"},
[Microsoft.Open.AzureAD.Model.ResourceAccess]@{
Id = "7ab1d382-f21e-4acd-a863-ba3e13f7da61";
Type = "Role"}
}
Write-Host -ForegroundColor Green "Creating the Azure AD application and related resources..."
if(!($App = Get-AzureADApplication -Filter "DisplayName eq '$($DisplayName)'" -ErrorAction SilentlyContinue))
{
$App = New-AzureADApplication -AvailableToOtherTenants $true -DisplayName $DisplayName -RequiredResourceAccess $graphAppAccess -ReplyUrls @("$AppReplyURI")
}
$password = New-AzureADApplicationPasswordCredential -ObjectId $app.ObjectId
$ApplicationId = $($app.AppId)
$ApplicationSecret = $($password.Value)
Write-Host "ApplicationId = $ApplicationId "
Write-Host "ApplicationSecret = $ApplicationSecret"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment