Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Login-AzureAD()
- {
- [CmdletBinding(PositionalBinding=$false)]
- param
- (
- [parameter(Mandatory=$false)]
- [guid] $TenantId = $tenId,
- [parameter(Mandatory=$false)]
- [guid] $ClientId = $cliid,
- [parameter(Mandatory=$false)]
- [string] $RedirectUri = $redUri
- )
- $url = "https://login.microsoftonline.com/{0}/oauth2/token" -f $TenantId
- $aadRes = 'https://graph.windows.net/'
- $grphRes = 'https://graph.microsoft.com/'
- $path = Resolve-Path -Path "$env:ProgramFiles\WindowsPowerShell\Modules\AzureAD*" -ErrorAction Stop
- $dll = Get-ChildItem -Path $path -Include Microsoft.IdentityModel.Clients.ActiveDirectory.dll -Recurse
- if (@($dll).Count -ne 1)
- {
- throw "Couldn't find the proper ActiveDirectory.dll!"
- }
- else
- {
- Import-Module $dll.PSPath -ErrorAction Stop -Global
- }
- $platform = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformParameters(
- [Microsoft.IdentityModel.Clients.ActiveDirectory.PromptBehavior]::Auto
- )
- $ErrorActionPreference = "Stop";
- $authContext = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext($url, $true)
- $task = $authContext.AcquireTokenAsync($aadRes, $ClientId, $RedirectUri, $platform)
- $task.Wait()
- if ($task.Status -ne "RanToCompletion")
- {
- return
- }
- $aadToken = $task.Result.AccessToken
- $userId = $task.Result.UserInfo.UniqueId
- $graphTask = $authContext.AcquireTokenSilentAsync($grphRes, $ClientId)
- $graphTask.Wait()
- if ($graphTask.Status -ne "RanToCompletion")
- {
- return
- }
- $graphToken = $graphTask.Result.AccessToken
- $graphToken | Set-Clipboard
- $manifest = Get-ChildItem -Path $dll.PSParentPath *.psd1 -File
- Import-Module $manifest.PSPath -ErrorAction Stop -Global
- Connect-AzureAD -TenantId $TenantId -AccountId $userId -AadAccessToken $aadToken -MsAccessToken $graphToken
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement