2

I am using Azure Automation account to copy my production database to staging. I also do a lot of custom stuff with this copied db + I do some migration stuff with Azure Storage accounts. To sum up, my 'master' runbook calls another runbooks and all the workflow is quite big so I would like to log every step.

Is there any way to use Application Insights for this purpose? If you know any other possible solutions let me know! I'm looking for the most simple and smart solution.

I found this module https://learn.microsoft.com/en-us/powershell/module/azurerm.applicationinsights/?view=azurermps-6.13.0 But it seems like there are no cmdlets to actually 'log' something.

I would like to be able to do something like this:

New-AzureRmApplicationInsightsLog -Name "myAppInsights" -LogText "Step 1. Start copying db"
silent
  • 14,494
  • 4
  • 46
  • 86
Sofia Bo
  • 679
  • 1
  • 10
  • 20
  • without import app insights dll, you can write powershell script as per this [doc](http://apmtips.com/blog/2017/03/27/oneliner-to-send-event-to-application-insights/) to send data to app insights, but it's a little complex. – Ivan Glasenberg Aug 22 '19 at 07:16
  • I do not mind to import App Insights dll and I actually tried to but I never succeded in it. I tried all options listed here https://vnextengineer.azurewebsites.net/powershell-application-insights/ and here https://stackoverflow.com/questions/35870749/azure-runbook-load-net-assembly-for-application-insight and here https://stackoverflow.com/questions/43265741/azure-automation-powershell-runbook-silently-fails-to-loadassembly/43380852#43380852 But I always get an error that the path is invalid. It works all right if I do it on my laptop but when I do it in Azure runbook it fails – Sofia Bo Aug 22 '19 at 07:53
  • If you can import module in your automation account, you can see my answer below. – Ivan Glasenberg Aug 23 '19 at 01:45

2 Answers2

4

Assume you can get the Microsoft.ApplicationInsights.dll. If you don't know how to get it, please let me know.

Then follow the steps below:

  1. Put the Microsoft.ApplicationInsights.dll to a zip file (you can right click the .dll file → send to → Compressed (zipped) folder), then you get a zip file named Microsoft.ApplicationInsights.zip

  2. Nav to azure portal → your automation account → Modules → Add a module: upload the zip file in step 1.

    Note: this may take a few minutes, when you see the status is available for Microsoft.ApplicationInsights, then it's ok.

  3. After the module is imported, the Microsoft.ApplicationInsights.dll is located at this location in azure: "C:\Modules\User\Microsoft.ApplicationInsights\Microsoft.ApplicationInsights.dll"

  4. Write your code in runbook to send telemetry data to application insights:

     Add-Type -Path "C:\Modules\User\Microsoft.ApplicationInsights\Microsoft.ApplicationInsights.dll"
    
     $InstrumentationKey = "xxxx"
     $TelClient = New-Object "Microsoft.ApplicationInsights.TelemetryClient"
     $TelClient.InstrumentationKey = $InstrumentationKey
    
     $TelClient.TrackEvent("PowerShell rocks!")
     $TelClient.Flush()
    
     write-output "ok it is finished."
    
  5. Check if you can see the data in application insights:

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Ivan Glasenberg
  • 29,865
  • 2
  • 44
  • 60
  • Thank you so much! I'll try and will let you know how it goes! – Sofia Bo Aug 23 '19 at 07:29
  • @SofiaBo, just wanna know that have you verified it? – Ivan Glasenberg Aug 27 '19 at 02:19
  • I had another work assignments during the previous days but today I'm going to try implementing it. I'll let you now right away! – Sofia Bo Aug 27 '19 at 08:16
  • I finally was able to try this out! Unfortunately, something definitely went wrong. I also put the code you listed above in InlineScript{...} because I was getting errors. But when I went to application insights I was not able to see any logs ... – Sofia Bo Aug 27 '19 at 15:34
  • Oh, wait! It worked, but with a quite big delay...I mean, the timestamp is accurate but I was not able to see it in logs for another 4-5 mins – Sofia Bo Aug 27 '19 at 15:56
  • @SofiaBo, the delay is by design. The telemetry data is not sent immediately, and the server also needs time to aggregate the data. – Ivan Glasenberg Aug 28 '19 at 00:57
  • That's ok as far as the timestamp is accurate. – Sofia Bo Aug 28 '19 at 07:41
0

I think you mean something like this?


$workspaceId = (Get-AzureRmOperationalInsightsWorkspace -ResourceGroupName $ResourceGroupLA -Name $WorkspaceName).ResourceId

Set-AzureRmDiagnosticSetting -ResourceId $automationAccountId -WorkspaceId $workspaceId -Enabled $true

Basically you just configure your entire automation account to use the loganalytics workspace. You don't do the logging from within the runbook as far as I know.

Marco
  • 525
  • 4
  • 17
  • Thank you! Is it suitable for tracking the runbooks' workflow? If one of the runbooks that I call from a 'master' runbook throws exception will it be shown there? – Sofia Bo Aug 21 '19 at 11:46
  • I tried this out and... I actually could not do this with a Free Trial subscription. Well, I'm still looking for a way to log something from runbook to Application Insights. This one is quite close to what I'm looking for https://stackoverflow.com/questions/37039586/azure-automation-logging-to-application-insight But I still wanna be able to run it fully in cloud without having to download anything to my local computer – Sofia Bo Aug 21 '19 at 15:42
  • As for the exception of other runbooks; Yes, so long as they are in the same automation account. Unfortunate you've got a free subscription though. I don't have anything else to provide you with in that case, except for my thoughts on what you found. I believe you could use a storage account to put the file on that you need. Then possibly use it from there? Automation can have access to Azure services: https://learn.microsoft.com/en-us/azure/automation/automation-hybrid-runbook-worker – Marco Aug 22 '19 at 05:54