question

71824798 avatar image
0 Votes"
71824798 asked tbgangav-MSFT answered

About the operation of [Stop Azure V2 VMs] in [Runbooks gallery]

The following error is output when executing [Stop Azure V2 VMs] in [Runbooks gallery].

<VM NAME> failed to stop. Error:

But actually
・The target VM is stopped
・ The job is completed
It is a state.

As far as you can see the edit screen
Because it meets the conditions of [$ ActivityOutput ['Stop VM']. Status -ne "Succeeded"] as shown below.
It seems that the above message is being output.

115169-runbooks%E5%95%8F%E3%81%84%E5%90%88%E3%82%8F%E3%81%9B.png

Is there anything that could be the cause?


azure-automation
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

tbgangav-MSFT avatar image
1 Vote"
tbgangav-MSFT answered

Hi @71824798,

I believe this question is a duplicate of this one. Let me know if that's not the case.

The only difference I could see is, its w.r.t powershell workflow runbook but in the other question its w.r.t powershell runbook but the answer aligns in the same lines. So, please check the answer and let me know if you have any further queries w.r.t it.

The probable reason for showing up 'test-vm-shimizu failed to stop' in output log stream is because runbook had the below highlighted line in it i.e., using write-output cmdlet. You may remove that line if you don't want to get notified about it in output log stream.

115094-image.png

About your other question on if VM is stopped successfully but why did you see failed to stop error then in that case I would suggest to have a post validation section in the script to check (using Get-AzVM cmdlet) whether the VM is currently in stopped state or not. This would be first step to try troubleshooting this issue with this script.

Just FYI, as mentioned here, the Az PowerShell is the replacement of AzureRM PowerShell and is the recommended version to use for interacting with Azure. To give little more context, starting in December 2018, the Azure PowerShell Az module is in general release and is now the intended PowerShell module for interacting with Azure. So Az modules are latest recommended ones to use and AzureRM modules are the older ones. Hence I would recommend you to not use "Stop Azure V2 VMs" runbook but mimic the same with Az PowerShell modules cmdlets something like shown below.

 param (
     [Parameter(Mandatory=$false)] 
     [String] $ResourceGroupName
 )
    
 # Ensure that the runbook does not inherit an AzContext
 Disable-AzContextAutosave –Scope Process
    
 # Connect to Azure with Run As account
 $ServicePrincipalConnection = Get-AutomationConnection -Name 'AzureRunAsConnection'
    
 Connect-AzAccount `
     -ServicePrincipal `
     -Tenant $ServicePrincipalConnection.TenantId `
     -ApplicationId $ServicePrincipalConnection.ApplicationId `
     -CertificateThumbprint $ServicePrincipalConnection.CertificateThumbprint
    
 $AzureContext = Set-AzContext -SubscriptionId $ServicePrincipalConnection.SubscriptionID
    
 # 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-AzVM -ResourceGroupName $ResourceGroupName
 }
 else 
 { 
  $VMs = Get-AzVM
 }
    
 # Stop each of the VMs
 foreach ($VM in $VMs)
 {
  $StopRtn = $VM | Stop-AzVM -Force -ErrorAction Continue
    
  if ($StopRtn.Status -ne 'Succeeded')
  {
  # The VM failed to stop, so send notice
         Write-Error ($VM.Name + " failed to stop. Error was:") -ErrorAction Continue
  Write-Error (ConvertTo-Json $StopRtn.Error) -ErrorAction Continue
  }
  else
  {
  # The VM stopped, so send notice
  Write-Output ($VM.Name + " has been stopped")
  }
 }

I have tested the above provided runbook and below are the screenshots which clarify that it works without any issues.

115111-capture2.png
115039-capture.png

Note that the runbook would successfully work only if you import all the required modules before executing the runbook. Please refer this document on how to import and manage modules in Azure Automation.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.