【问题标题】:Binding Redirection in Azure Automation Account PowerShell RunbookAzure 自动化帐户 PowerShell Runbook 中的绑定重定向
【发布时间】:2020-12-09 21:36:47
【问题描述】:

在 Azure 自动化帐户运行手册中设置 PowerShell 绑定重定向时需要一些帮助。

基本上,我的 Runbook 调用了两个第 3 方 .Net dll 中的许多方法,它们都由同一作者提供。其中一个 dll 依赖于 Newtonsoft.Json V12.0.1,另一个依赖于 IdentityModel V4.0.0,而 IdentityModel V4.0.0 又依赖于 Newtonsoft.Json V11.0.2。我的 Azure 环境使用 Windows PowerShell Desktop V5.1.15063.726。在我的 Runbook 中做任何工作之前,我会调用一个函数来加载我导入的模块中的所有 dll(该函数为每个 dll 调用 [System.Reflection.Assembly]::LoadFrom() 方法)。我已验证我导入的 dll,包括 Newtonsoft.Json.dll V12.0.1 已成功加载。

正如预期的那样,当 Runbook 执行遇到调用需要 IdentityModel.dll 的第 3 方 dll 方法之一的行时,将引发异常: 使用“0”参数调用“GetResult”的异常:“无法加载文件或程序集'Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'或其依赖项之一。系统找不到指定的文件。” System.IO.FileNotFoundException。

这是一个已知问题,到目前为止的共识似乎是我需要在 PowerShell 中创建并附加一个事件处理程序。可以在here 找到建议的解决方案之一。我的问题在于尝试附加处理程序的行:

[System.AppDomain]::CurrentDomain.add_AssemblyResolve($onAssemblyResolveEventHandler)

当我的 Runbook 遇到这一行时,会引发以下异常: 找不到“add_AssemblyResolve”的重载和参数计数:“1”。 TargetSite: Void CheckActionPreference(System.Management.Automation.Language.FunctionContext, System.Exception) StackTrace: 在 System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception 异常)

我可以确认,非常有限的测试似乎证明代码在使用 PowerShell ISE 的 Windows 8.1 中有效。我进行了广泛搜索,以查看在 Azure 和 Windows 桌面中编写此代码的方式是否存在差异,但没有运气。谁能看到我做错了什么?我是否缺少任何 DLL 或使用 PowerShell 脚本中的语句?如果无法以这种方式重定向绑定,任何人都可以提供替代技术吗?

显示事件处理程序的创建和附加的代码摘录如下:

# Intercept resolution of binaries
$onAssemblyResolveEventHandler = [System.ResolveEventHandler]
{
    param($sender, $e)

    Write-Host "ResolveEventHandler: Attempting FullName resolution of $($e.Name)" 
    foreach($assembly in [System.AppDomain]::CurrentDomain.GetAssemblies()) 
    {
        if ($assembly.FullName -eq $e.Name)
        {
            Write-Host "Successful FullName resolution of $($e.Name)" 
            return $assembly
        }
    }

    Write-Host "ResolveEventHandler: Attempting name-only resolution of $($e.Name)" 
    foreach($assembly in [System.AppDomain]::CurrentDomain.GetAssemblies())
    {
        # Get just the name from the FullName (no version)
        $assemblyName = $assembly.FullName.Substring(0, $assembly.FullName.IndexOf(", "))

        if ($e.Name.StartsWith($($assemblyName + ","))) 
        {
            Write-Host "Successful name-only (no version) resolution of $assemblyName" 
            return $assembly
        }
    }

   Write-Host "Unable to resolve $($e.Name)" 
    return $null
}   
# Attach event handler
# This is the line that fails.
[System.AppDomain]::CurrentDomain.add_AssemblyResolve($onAssemblyResolveEventHandler)

【问题讨论】:

    标签: azure powershell redirect binding


    【解决方案1】:

    我无法解决在 Azure 自动化帐户 - PowerShell runbook 环境中工作的绑定重定向问题,但两个第 3 方 .Net dll 的作者已同意使用 NewtonSoft.Json 11.0 版。 2 对于两个 dll,所以我的问题消失了。

    【讨论】:

    • 您好,如果您解决了您的问题,您应该将您自己的答案标记为已接受。
    猜你喜欢
    • 2020-07-24
    • 2021-07-17
    • 1970-01-01
    • 2021-05-25
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    相关资源
    最近更新 更多