Skip to content

Instantly share code, notes, and snippets.

@darrenjrobinson
Created April 12, 2017 01:56
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 darrenjrobinson/d294362808845435ebf8f426493102a1 to your computer and use it in GitHub Desktop.
Save darrenjrobinson/d294362808845435ebf8f426493102a1 to your computer and use it in GitHub Desktop.
Powershell AuthN to AzureAD using AzureADPreview PSM Helper Lib
# ******************************************************************************
# the default path to where the ADAL GraphAPI PS Module puts the Libs
# Obtained using the AzureADPreview PSM
# Adding the AD library to your PowerShell Session.
Add-Type -Path 'C:\Program Files\WindowsPowerShell\Modules\AzureADPreview\2.0.0.52\Microsoft.IdentityModel.Clients.ActiveDirectory.dll'
# This is the tenant id of you Azure AD. You can use tenant name instead if you want.
$tenantID = "customer.com.au"
$authString = "https://login.microsoftonline.com/$tenantID"
# Here, the username must be MFA disabled user Admin at least, and must not be a live id.
$username = "user@customer.com.au"
$password = "ssshS3cret"
# The resource URI for your token.
$resource = "https://graph.windows.net/"
#$resource = "https://graph.microsoft.com/"
# This is the common client id.
$client_id = "1950a258-227b-4e31-a9cf-717495945fc2"
# Create a client credential with the above common client id, username and password.
$creds = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.UserCredential" `
-ArgumentList $username,$password
# Create a authentication context with the above authentication string.
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" `
-ArgumentList $authString
# Acquire access token from server.
$authenticationResult = $authContext.AcquireToken($resource,$client_id,$creds)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment