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.
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.
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.