I’ve been working with Azure Automation in the last few days, and during the creation of the code I was using PowerShell ISE to backup and restore Azure Key Vault secrets and keys between Azure Key Vaults.
When I moved my code to Azure Automation, I noticed the error Operation returned an invalid status code only when executing the Backup-AzureKeyVaultSecret cmdlet. Initially, I thought was something related to permissions, but after some troubleshooting I removed the permission from the picture.
The issue was related to modules, when compared the modules that I had in my machine against the Hybrid Worker VM, I noticed that Azure Key Vault module 4.3.0 has no issues running the Backup-AzureKeyVaultSecret , however the same cmdlet fails when running on the latest version 5.0.0.
If that is your case, I recommend first comparing the modules loaded, by running Get-Module, we can always check all modules using Get-Module –ListAvailable which will provide the output of all modules and their respective folder.
In order to install a specific module version, we can use the following cmdlet:
Install-Module AzureRM.KeyVault –MaximumVersion 4.3.0 –Force
In my scenario, the AzureRM.KeyVault had to be 4.3.0, and after installing the module, we have a couple of ways to remove non-required modules.
The first method is going to the folder where the module has been installed, and remove the folder.
My preferred method is using PowerShell, we can use Uninstall-Module and combine with –RequiredVersion to remove the specific module. The process to list the existent modules, remove and list them again is shown in the image below.