Skip to content

Instantly share code, notes, and snippets.

@devblackops
Created September 18, 2019 19:59
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 devblackops/b734801c2379e6292a941c261b6fdfed to your computer and use it in GitHub Desktop.
Save devblackops/b734801c2379e6292a941c261b6fdfed to your computer and use it in GitHub Desktop.
$tenant = '<tenant-id>'
$subscription = '<subscription-id>'
$clientId = '<client-id>'
$clientSecret = '<client-secret>'
# Log into Azure w/ Service Principal
$creds = [pscredential]::new($clientId, ($clientSecret | ConvertTo-SecureString -AsPlainText -Force))
Connect-AzAccount -TenantId $tenant -SubscriptionId $subscription -Credential $creds -ServicePrincipal > $null
# Get auth token
$ctx = Get-AzContext
Get-AzADApplication > $null # Fill token cache
$token = $ctx.TokenCache.ReadItems() | Where-Object {$_.Resource -eq 'https://graph.windows.net/' -and $_.ExpiresOn -gt (Get-Date)}
# Log into AzureAD with token
AzureAD.Standard.Preview\Connect-AzureAD -TenantId $tenant -AccountId $ctx.Account.Id -AadAccessToken $token.AccessToken

$error

AzureAD.Standard.Preview\Connect-AzureAD : Could not load file or assembly 'Microsoft.IdentityModel.Clients.ActiveDirectory, Version=3.19.8.16603, Culture=neutral, PublicKeyToken=31
bf3856ad364e35'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621)
At line:1 char:1
+ AzureAD.Standard.Preview\Connect-AzureAD -TenantId $tenant -AccountId ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [Connect-AzureAD], FileLoadException
+ FullyQualifiedErrorId : System.IO.FileLoadException,Microsoft.Open.Azure.AD.CommonLibrary.ConnectAzureAD

$Error[0].Exception

Could not load file or assembly 'Microsoft.IdentityModel.Clients.ActiveDirectory, Version=3.19.8.16603, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Could not find or load a s
pecific file. (Exception from HRESULT: 0x80131621)

$Error[0].Exception.StackTrace

   at Microsoft.Open.Azure.AD.CommonLibrary.AzureSession.NewSessionstate()
   at Microsoft.Open.Azure.AD.CommonLibrary.ConnectAzureAD.BeginProcessing() in C:\source\src\dev\PowerShell.Core\CommonLibrary\ConnectAzureAD.cs:line 121
   at System.Management.Automation.Cmdlet.DoBeginProcessing()
   at System.Management.Automation.CommandProcessorBase.DoBegin()
@ziesemer
Copy link

Did anything further ever come of this?
I think I just ran across something similar: Azure/azure-powershell#11446

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment