Forked from darrenjrobinson/AuthNtoAzureADwithPShellandADALHelperLib.ps1
Created
February 5, 2020 12:56
-
-
Save Splaxi/05613a61be5a7f54e22d22f16f297042 to your computer and use it in GitHub Desktop.
AuthN to AzureAD using PowerShell and AzureAD PSM ADAL Helper Lib
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
# Hack to ignore versioning of the dll file and folder structure | |
$folderName = (Get-ChildItem -Path "C:\Program Files\WindowsPowerShell\Modules\AzureAD\2.0.*\Microsoft.IdentityModel.Clients.ActiveDirectory.dll").Directory.Name | Sort-Object -Descending | Select-Object -First 1 | |
# ADAL Helper Lib | |
Add-Type -Path "c:\Program Files\WindowsPowerShell\Modules\AzureAD\$folderName\Microsoft.IdentityModel.Clients.ActiveDirectory.dll" | |
# Azure tenant | |
$tenant = "" | |
# Application ID for Powershell client | |
$client_Id = "1950a258-227b-4e31-a9cf-717495945fc2" | |
# Login URI | |
$authority = "https://login.microsoftonline.com/$tenant" | |
#redirect uri of powershell | |
[uri]$redirectUri = "urn:ietf:wg:oauth:2.0:oob" | |
# API URL | |
$resource = "https://management.azure.com" | |
# Username and Password | |
$username = "" | |
$password = ConvertTo-SecureString "" –asplaintext –force | |
$credentials = New-Object System.Management.Automation.PSCredential $Username, $password | |
# Endpoint | |
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authority | |
# Credentials to connect | |
$AADcredential = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.UserPasswordCredential" -ArgumentList $credentials.UserName, $credentials.Password | |
# AuthN and get token | |
$authenticationResult = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContextIntegratedAuthExtensions]::AcquireTokenAsync($authContext, $resource, $client_Id, $AADcredential).result | |
# create AuthN Header | |
$BearerToken = $authenticationResult.CreateAuthorizationHeader() | |
$BearerToken |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment