Azure Automation Account Webhook Response

455 views Asked by At

I'm trying to execute PowerShell script using webhook in Azure automation account. My Powershell script will perform shutdown the VM through webhook. I'm getting the response as Job id, so my requirement is to get the response as given VmName $VmName should be printed and with stopped successfully message along with the job id. How to get that response using script where should I added to get the response.

enter image description here

Param
(
[Parameter (Mandatory = $false)]
[object] $WebhookData
)

if ($WebhookData) {
$WebhookName = $WebHookData.WebhookName
$WebhookHeaders = $WebhookData.RequestHeader
$WebhookBody = $WebhookData.RequestBody
$input = (ConvertFrom-Json -InputObject $WebhookData.RequestBody)
}

# Login to Automation Account
$connectionName = 'AzureRunAsConnection'

try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection= Get-AutomationConnection -Name $connectionName

"Logging in to Azure..."
#Add-AzureRMAccount 
Connect-AzAccount -ServicePrincipal -TenantId $servicePrincipalConnection.TenantId -ApplicationId $servicePrincipalConnection.ApplicationId -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint -WarningAction Ignore
}
catch {

if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}

$ResourceGroupName = $input.ResourceGroupName
$VmName = $input.VmName


Stop-AzVM -Name $VmName -ResourceGroupName $ResourceGroupName  -Force

Write-Output "VM $VmName in Resource Group $ResourceGroupName was stopped Successfully"

1

There are 1 answers

1
Harikrishnan On

if you are trying validate the VM status inside the script you can check below


$Stop = Stop-AzVM -Name $VmName -ResourceGroupName $ResourceGroupName  -Force
if(($Stop.Status) -eq "Succeeded"){
Write-Output "VM $VmName in Resource Group $ResourceGroupName was stopped Successfully"
}
# Or can check VM status seperatly by using 
Get-AzVM $VmName -Status |Select-Object Name,PowerState