What are some considerations when adapting a locally run PowerShell script that invokes web requests to Azure Automation? This post qualifies as one where I want to document steps for my own benefit after I forget in a few weeks, but it’s even better if anyone happens to be doing something similar and benefits as well.
I’ve recently been working with my BlueGranite colleague and Microsoft MVP Melissa Coates on getting data from the Office 365 Management API. The specific use case is to obtain Power BI’s audit logs, which are available as RecordType 20 under Audit.General. While this post will not go into detail on the exact solution or share that code, the basics originated from this Microsoft repository: https://github.com/OfficeDev/O365-InvestigationTooling
The focus here is to document two specific steps for adapting a locally executed PowerShell script for an Azure Automation runbook. In addition to standard work in Automation to add credentials/connections and create parameters where needed, the following changes were required to adapt a local script for my runbook.
1) Add -UseBasicParsing to Web Requests
The Management API script is filled with various requests using Invoke-WebRequest. Initially, I received numerous errors stating that the Internet Explorer engine is not available.
Invoke-WebRequest : The response content cannot be parsed because the Internet Explorer engine is not available, or Internet Explorer's first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again. At line:46 char:9 + $subs = Invoke-WebRequest -Headers $headerParams -Uri "https://manage ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotImplemented: (:) [Invoke-WebRequest], NotSupportedException + FullyQualifiedErrorId : WebCmdletIEDomNotSupportedException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
To overcome this, I needed to add the parameter UseBasicParsing. In PowerShell 6, this should no longer be necessary. Since Automation currently runs 5.1.15063.726 though, it is. Locally, this parameter is not needed even running PS version 5.x.
Original: $oauth = Invoke-RestMethod -Method Post -Uri $loginURL/$tenantdomain/oauth2/token?api-version=1.0 -Body $body
Adapted: $oauth = Invoke-RestMethod -Method Post -Uri $loginURL/$tenantdomain/oauth2/token?api-version=1.0 -Body $body –UseBasicParsing
2) Alter $ProgressPreference
PowerShell shows progress using $ProgressPreference, which defaults to “continue”. For Automation, I needed to change the option to “silently continue” and suppress progress bars. Locally, the progress can be convenient to see, but it was causing issues with Automation.
Nice article. I found one more: You can’t use the -SkipHttpErrorCheck parameter which was introduced in PowerShell 7
Thanks for passing along that knowledge!