Getting error in azure runbook

Biswajeet Kumar 116 Reputation points
2021-01-22T07:57:08.623+00:00

I am using the this simple code to start a VM in azure from Azure Runbook but getting the error. I am using AzureAD user as cred.

59510-screenshot-2021-01-22-132538.png

param (
[Parameter(Mandatory=$false)]
[String] $AzureCredentialAssetName = 'AzureCredential',

    [Parameter(Mandatory=$false)]  
    [String] $AzureSubscriptionIdAssetName = 'AzureSubscriptionId',  
  
    [Parameter(Mandatory=$false)]   
    [String] $ResourceGroupName  
)  
  
# Returns strings with status messages  
[OutputType([String])]  
  
# Connect to Azure and select the subscription to work against  
$Cred = Get-AutomationPSCredential -Name $AzureCredentialAssetName -ErrorAction Stop  
  
Add-AzureRmAccount -Credential $Cred -ErrorAction Stop -ErrorVariable err  
if($err) {  
 throw $err  
}  
  
$SubId = Get-AutomationVariable -Name $AzureSubscriptionIdAssetName -ErrorAction Stop  
  
# If there is a specific resource group, then get all VMs in the resource group,  
# otherwise get all VMs in the subscription.  
if ($ResourceGroupName)   
{   
 $VMs = Get-AzureRmVM -ResourceGroupName 'test'  
}  
else   
{   
 $VMs = Get-AzureRmVM  
}  
  
# Start each of the VMs  
foreach ($VM in $VMs)  
{  
 $StartRtn = $VM | Start-AzureRmVM -ErrorAction Continue  
  
 if ($StartRtn.Status -ne 'Succeeded')  
 {  
 # The VM failed to start, so send notice  
        Write-Output ($VM.Name + " failed to start")  
        Write-Error ($VM.Name + " failed to start. Error was:") -ErrorAction Continue  
 Write-Error (ConvertTo-Json $StartRtn.Error) -ErrorAction Continue  
 }  
 else  
 {  
 # The VM stopped, so send notice  
 Write-Output ($VM.Name + " has been started")  
 }  
}  
Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,144 questions
0 comments No comments
{count} votes

Accepted answer
  1. Stanislav Zhelyazkov 21,521 Reputation points MVP
    2021-01-22T08:16:54.05+00:00

    Hi,
    Perhaps that user does not have permissions? Best is to use Service Principal for these things though as the user might be required to login with MFA which you will not be able to do in non-interactive approach like Azure Automation.

    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.

    0 comments No comments

0 additional answers

Sort by: Most helpful