"Invalid directory on URL" 尝试使用在 Powershell 中使用 New-PSDrive 创建的路径

"Invalid directory on URL" while trying to use path created using New-PSDrive in Powershell

我写了一个函数来获取身份验证令牌。我使用 New-PSDrive 因为 PathTooLongException 被抛出。 Write-Host $adal, (Test-Path $adal) returns True 同时测试路径是否存在。但在 LoadFrom() 中抛出异常。有人可以帮助我消除错误吗?

代码:

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."
    }
}

异常:

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)

感谢@mclayton 告知根本原因。

使用 Add-Type 而不是 LoadForm() 在 powershell 中加载程序集。

Add-Type -Path $adal