Azure Automation Powershell runbook silently fails

2019-09-14 20:23发布

问题:

I'm new to Powershell Runbook, so forgive me if I'm missing something obvious. I'm trying to log an Application Insights request from my script, but can't even get the DLL to load, though I've seen other code out there that does something very similar. NOTE that this is a Powershell Runbook, not a Powershell Workflow Runbook.

Here's my code:

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"

And here's the output I get when running it in the Test 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

It seems to get as far as the LoadAssembly and then craps out, running the script three times before giving up. Any ideas what I'm doing wrong? The DLL clearly exists at that location, and I'm not getting any error output to help me debug. Thanks!

回答1:

It looks like your call to LoadFrom is generating a massive amount of output. You can see this if you run your code interactively and just change it like this: [System.Reflection.Assembly]::LoadFrom($assemblyPath) | Out-String -Width 500000000, it will actually generate an OutOfMemoryException. Alternatively, if you modify your runbook like this: [System.Reflection.Assembly]::LoadFrom($assemblyPath) | Out-Null, your job will run. Right now, this large amount of output is crashing the runtime. (This is probably a bug in the runbook execution engine.)

However, don't do that! LoadFrom, LoadPartial, etc... these are deprecated in PowerShell 3.

The good thing is there is a non-deprecated PowerShelly way to do what you want. Just use Add-Type -Path $assemblyPath instead of [System.Reflection.Assembly]::LoadFrom($assemblyPath).

As an FYI, whenever you see your job suspended and the message, "The job action 'Activate' cannot be run, because the process stopped unexpectedly. The job action was attempted 3 times." - this means you have totally crashed the runtime and your entire job environment. :) We try 3 times just in case it is something we did wrong loading your script or building the environment, but after 3 times we figure it is a bad script.