I have a web scraping and HTTP posting automation written in PowerShell that I developed partially locally and partially on my hybrid worker with the authoring toolkit. During development and testing on the hybrid worker server, it run as expected in a few hours but when the code runs on the hybrid worker from the automation account, the code is considerably slower.
There are 2 primary sections that are slowing it down in the automation account and taking around 50 seconds each, and these have to run tens of thousands of times throughout the automation:
$unparsedJSON = ($detailsSession.Scripts)[-1].outerHTML
There are only 31 script blocks in the $detailsSession and the script block is 806 character. This takes 0.77 millisecond on the server.
$linkedRecord = ($linkedSession.allelements.innertext | Where-Object {$_ -like 'Record -*'})
There are 419 elements in the $linkedSession. This line takes 13 milliseconds directly on the server.
To optimize the JSON parsing process for
$detailsSession.Scripts, leveraging theConvertFrom-Jsoncmdlet is recommended over direct property access. Given that there are only 31 script blocks, each averaging around 806 characters, the parsing operation should not be excessively slow. By employingConvertFrom-Json, the parsing process can be streamlined and potentially enhanced for improved efficiency.When dealing with large datasets like the 419 elements in
$linkedSession, filtering elements usingWhere-Objectcan introduce performance bottlenecks. To address this, consider leveraging XPath for optimization, particularly if the structure of the elements allows for it. XPath offers a more efficient approach to filtering based on specific patterns.Optimize looping by utilizing
foreachoverForEach-Objectfor improved speed. Additionally, leverage PowerShell's parallel processing capabilities, such as jobs or runspaces, to execute independent tasks concurrently, thereby boosting overall performance.Powershell code:-
Output:-
You can also start the runbook by using the Powershell command here to get more insights on the Run.