Ansible, PowerShell and Output from Cmdlets with ProgressPreference

I’ve posted previously regarding returning output from PowerShell scripts that have been invoked by Ansible playbooks:

https://www.jonathanmedd.net/2019/10/ansible-windows-and-powershell-the-basics-part-6-displaying-output-from-powershell-code.html

https://www.jonathanmedd.net/2019/07/returning-data-from-powershell-scripts-to-be-consumed-by-ansible-playbooks.html

I encountered an issue recently where noise from a particular PowerCLI cmdlet, Invoke-VMScript, was polluting the output returned from the PowerShell script to Ansible. Specifically it looked like the below, a mess of Invoke-VMScript across the screen:

This is because Invoke-VMScript is one of those cmdlets which displays progress to the console while it is executing:

Since I was not interested in any progress info polluting the text output I was returning from PowerShell, I set the Preference Variable:

$ProgressPreference = ‘SilentlyContinue’

i.e. to hide any progress output in the script. The noise was then absent from the text output I was returning.