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.
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")
}
}