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.
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"
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.