0
votes

I have an Azure runbook that runs on schedule. Its in powershell and this runbook starts a VM and executes a script on the VM started. How I achieve this is

1) Store the script to be run on the VM in a storage account 2) Run powershell runbook 3) Powershell runbook uses wget command to copy the script from step 1 4) Invoke-AzureRmVMRunCommand in the Azure automation powershell commands as shown below

wget "https://utilitystorageaccnt.blob.core.windows.net/utilitycontainer/token" -outfile ((Get-Location).path + "\Reporting Copy.ps1") -UseBasicParsing

Invoke-AzureRmVMRunCommand -ResourceGroupName $ResourceGroupName -VMName $VmName -CommandId 'RunPowerShellScript' -ScriptPath ((Get-Location).path + '\Reporting Copy.ps1') -ErrorVariable result

Please not that the above two commands are in the powershell runbook script and not the actual script that is run on the VM.

Facing two issues

1) When this script Reporting Copy.ps1 runs standalone on the VM, then it works properly and it has no issues. When it is run using the runbook, I get these errors in the log file on the target vm.

"New-AzStorageContext : The term 'New-AzStorageContext' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."

2) Even after this error occurs, it doesnt terminate and runs in loops. This script does a copy operation and it keeps looping until all the copy is complete. I can handle code to terminate but I would like to know how to force terminate a runbook. I tried to stop the VM for even a hour and it resumes the copy operation. The runbook status in Azure shows as completed. There are two python processes that show in explorer and terminating them doesn't work either.

Any help or hint is appreciated.

Thanks.

2

2 Answers

0
votes

Look like you did not imported Az PowerShell module into our Automation Account.

Please, follow this tutorial : Az module support in Azure Automation

Try to use only Az module and not AzureRM

0
votes

The issue was because I had not installed the AZ module for all users like this.

Install-Module -Name Az -AllowClobber -Scope AllUsers

Instead I had used

Install-Module -Name Az -AllowClobber -Scope CurrentUser

and since the automation runs on a different user, the issue occurred. Thanks for your help.