【发布时间】:2020-01-03 17:53:35
【问题描述】:
我编写了一个函数来获取身份验证令牌。我使用了New-PSDrive,因为PathTooLongException 被抛出。 Write-Host $adal, (Test-Path $adal) 在测试路径是否存在时返回 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)
【问题讨论】:
-
来自docs.microsoft.com/en-us/powershell/module/…... “因为临时驱动器只有 PowerShell 知道,所以您无法使用文件资源管理器、Windows Management Instrumentation (WMI)、组件对象模型 (COM) 来访问它们, Microsoft .NET Framework,或使用 net use 等工具。”。 LoadFrom 是一种 .Net Framework 方法,因此它无法理解您的“CustomDrive:”是什么。
标签: c# powershell .net-assembly new-psdrive