3

I'm working on an Azure Automation script where I need to retrieve an access token to call the AAD Graph API. I wanted to use ADAL to do this so I zipped up Microsoft.IdentityModel.Clients.ActiveDirectory.dll and uploaded it as a module. When I run from the test blade, it sometimes works, and sometimes fails with this error:

New-Object : Cannot find type [Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential]: verify that the 
assembly containing this type is loaded.
At line:22 char:9
+ $cred = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredent ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

When it fails I just run it again and it works. Now I've published and scheduled this runbook as a daily job 3 days ago. So far it has failed every day with this same error.

Has any one else seen this? Any suggestions on next steps?

BenV
  • 12,052
  • 13
  • 64
  • 92

1 Answers1

1

I fixed this by adding the following line to my script:

Add-Type -Path "C:\Modules\User\Microsoft.IdentityModel.Clients.ActiveDirectory\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"

Still not sure why it was working intermittently before. I guess depending on what else was going on ADAL may or may not have been loaded?

BenV
  • 12,052
  • 13
  • 64
  • 92
  • I just grabbed the dll from the SDK. – BenV Dec 16 '15 at 19:40
  • Could you share your Module? – Kode Jul 14 '17 at 17:26
  • @Kode You can now import the ADAL module from the Gallery directly in the Azure portal - no need to manually upload it any more. – BenV Jul 17 '17 at 15:45
  • I imported the ADAL module but it didn't work for me. It has two PowerShell calls but my SQL Connection fails. Can you provide an example of how you connect via PowerShell? – Kode Jul 17 '17 at 16:08
  • Here is my post with connection attempt: https://stackoverflow.com/questions/45091135/active-directory-password-connection-azure-sql-failure-from-azure-automation/45093784#45093784 – Kode Jul 17 '17 at 16:11