0
votes

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
@BenH thanks for the response, I'd actually seen that post, and I should've mentioned that I tried that path also, but the assembly didn't even exist there: dir : Cannot find path 'C:\Modules\Azure\Microsoft.ApplicationInsights.dll' because it does not exist.UnionP
I was able to workaround my issue by switching to a Powershell Runbook, and enclosing all my .NET calls in InlineScript blocks. Not the most elegant, but it works. Would still love to hear if there's a way to get this working without having to do thatUnionP
well, try doing try-catch and outputting error?4c74356b41
@4c74356b41 That doesn't help unfortunately. If I DO get an exception, like FileNotFound if I pass a bad path to LoadAssembly, it does display it, this appears to be something else entirelyUnionP

1 Answers

3
votes

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.