我是 Powershell Runbook 的新手,所以如果我遗漏了一些明显的内容,请原谅我。我正在尝试从我的脚本记录 Application Insights 请求,但甚至无法加载 DLL,尽管我已经看到其他代码执行非常类似的操作。请注意,这是 Powershell Runbook,而不是 Powershell 工作流 Runbook。
这是我的代码:
Write-Output "Starting"
$assemblyPath = "C:\Modules\Global\Azure\Compute\Microsoft.ApplicationInsights.dll"
dir $assemblyPath
Write-Output "1"
[System.Reflection.Assembly]::LoadFrom($assemblyPath)
Write-Output "2"
这是我在“测试” Pane 中运行它时得到的输出:
Starting
Directory: C:\Modules\Global\Azure\Compute
Mode LastWriteTime Length Name
---- ------------- ------ ----
------ 1/11/2016 1:59 PM 152824 Microsoft.ApplicationInsights.dll
1
Starting
Directory: C:\Modules\Global\Azure\Compute
Mode LastWriteTime Length Name
---- ------------- ------ ----
------ 1/11/2016 1:59 PM 152824 Microsoft.ApplicationInsights.dll
1
Starting
Directory: C:\Modules\Global\Azure\Compute
Mode LastWriteTime Length Name
---- ------------- ------ ----
------ 1/11/2016 1:59 PM 152824 Microsoft.ApplicationInsights.dll
1
它似乎到达了 LoadAssembly,然后就崩溃了,在放弃之前运行了脚本三遍。有什么想法我做错了吗? DLL 显然存在于该位置,并且我没有收到任何错误输出来帮助我调试。谢谢!
最佳答案
看起来您对 LoadFrom
的调用正在生成大量输出。如果您以交互方式运行代码并像这样更改它,您可以看到这一点: [System.Reflection.Assembly]::LoadFrom($ assemblyPath) | Out-String -Width 500000000
,它实际上会生成一个OutOfMemoryException
。或者,如果您像这样修改 Runbook: [System.Reflection.Assembly]::LoadFrom($ assemblyPath) | Out-Null
,您的作业将运行。目前,如此大量的输出正在导致运行时崩溃。 (这可能是 Runbook 执行引擎中的错误。)
但是,不要这样做! LoadFrom
、LoadPartial
等...这些在 PowerShell 3 中已弃用。
好处是有一种未弃用的 PowerShelly 方法可以完成您想要的操作。只需使用 Add-Type -Path $ assemblyPath
而不是 [System.Reflection.Assembly]::LoadFrom($ assemblyPath)
。
仅供引用,每当您看到作业暂停并显示消息“作业操作‘激活’无法运行,因为进程意外停止。作业操作已尝试 3 次。” - 这意味着您已经完全崩溃了运行时和整个工作环境。 :) 我们尝试了 3 次,以防万一我们加载脚本或构建环境时出错,但 3 次后我们发现这是一个错误的脚本。
关于powershell - Azure 自动化 Powershell Runbook 无法进行 LoadAssembly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43265741/