The issue caused by using both AzureRm and Az modules in the same session. So to resolve the issue AzureRm module was removed and script was modified to use only Az commands.
Installed Powershell 7.1.3
Uninstalled AzureRM Module.
Installed the AZ Module.
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force