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

清水雄樹 21 Reputation points
2021-07-16T02:23:21.707+00:00

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
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,256 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. tbgangav-MSFT 10,416 Reputation points
    2021-07-16T08:17:42.63+00:00

    Hi @清水雄樹 ,

    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.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.