0

I have written a function to get authentication token. I have used New-PSDrive because PathTooLongException was thrown. Write-Host $adal, (Test-Path $adal) returns True while testing if path exists. But throws exception in LoadFrom(). Could someone help me get rid of the error?

Code:

function GetAuthToken {
    try{
        Write-Host "GetAuthToken-Start"
        If (!(Test-Path CustomDrive:)){
            $adalPath = Get-Module -Name "AzureRM.Profile" -ListAvailable -All | Select -First 1 | Select -ExpandProperty ModuleBase
            New-PSDrive -Name "CustomDrive" -PSProvider filesystem -Root $adalPath
            Write-Host "Created CustomDrive."
        }

        $adal = "CustomDrive:\Microsoft.IdentityModel.Clients.ActiveDirectory.dll"
        $adalforms = "CustomDrive:\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll"

        Write-Host $adal, (Test-Path $adal) #Test-Path returns True
        Write-Host $adalforms, (Test-Path $adalforms)

        Write-Host "Loading required DLLs..."
        [System.Reflection.Assembly]::LoadFrom($adal) | Out-Null #This line throws exception
        [System.Reflection.Assembly]::LoadFrom($adalforms) | Out-Null
        Write-Host "Loaded required DLLs successfully."

        Write-Host "Trying to acquire token..."
        $authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" -ArgumentList $authorityUri
        $authResult = $authContext.AcquireToken($resourceUri, $clientId, $redirectUri, "Always") 
        Write-Host "Acquired token successfully."

        Write-Host $authResult
        Write-Host "GetAuthToken-End"
        return $authResult
    }
    catch{
        Write-Error -Message $_.Exception
        throw $_.Exception
    }
    finally{
        Remove-PSDrive -Name "CustomDrive"
        Write-Host "Removed CustomDrive."
    }
}

Exception:

GetAuthToken : System.Management.Automation.MethodInvocationException: Exception calling "LoadFrom" with "1" argument(s): "Invalid directory on 
URL." ---> System.ArgumentException: Invalid directory on URL.
   at System.Security.Util.DirectoryString.CreateSeparatedString(String directory)
   at System.Security.Util.URLString.ParseFileURL(String url)
   at System.Security.Util.URLString.GetFileName()
   at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, RuntimeAssembly 
reqAssembly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean 
suppressSecurityChecks)
   at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm 
hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
   at System.Reflection.Assembly.LoadFrom(String assemblyFile)
   at CallSite.Target(Closure , CallSite , Type , Object )
   --- End of inner exception stack trace ---
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
Shridhar R Kulkarni
  • 6,653
  • 3
  • 37
  • 57
  • From https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/new-psdrive?view=powershell-6... “Because temporary drives are known only to PowerShell, you can't access them by using File Explorer, Windows Management Instrumentation (WMI), Component Object Model (COM), Microsoft .NET Framework, or with tools such as net use.”. LoadFrom is a .Net Framework method so it doesn’t understand what your “CustomDrive:” is. – mclayton Jan 04 '20 at 02:58

1 Answers1

0

Thanks to @mclayton for letting know the root cause.

Used Add-Type instead of LoadForm() to load the assembly in powershell.

Add-Type -Path $adal
Shridhar R Kulkarni
  • 6,653
  • 3
  • 37
  • 57