通过 webhook Azure 启动/停止 VM

标签 azure azure-powershell

我们的 Azure 环境中有多个虚拟机,具有多个资源组。某些资源组具有多个虚拟机。我们现在使用 URL 触发器 webhook 来启动或停止 VM。这是可行的,但是当资源组包含多个虚拟机时,所有虚拟机都将启动,或者所有虚拟机都将停止,而不是您想要启动/停止的虚拟机。

尝试了多个脚本,但它不起作用或给我错误。

    param(
  [Parameter(Mandatory=$false)]
  [object] 
  $WebHookData
 )  

write output "Data WebHook $WebHookData"

#retrieve ResourceGroup
$ResourceGroupName = $WebHookData.RequestBody
write output "Data ResourceGroup $ResourceGroupName"

$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Connect-AzureRmAccount -ServicePrincipal -Tenant $Conn.TenantID -ApplicationId $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint

$VMs = Get-AzureRmVM -ResourceGroupName $ResourceGroupName

    if(!$VMs) 
    {
        Write-Output -InputObject 'No VMs were found in the specified Resource Group.'
    }
    else 
    {
        ForEach ($VM in $VMs) 
        {
            $StartVM = Stop-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VM.Name -Force #-ErrorAction SilentlyContinue


        } 
    } 

$message = ConvertTo-Json -Compress -InputObject ([ordered]@{
       headers = @{'content-type' = 'text/plain'}
          body = ''
    statusCode = 200
})

最佳答案

您可以尝试以下脚本来启动/停止虚拟机。

启动虚拟机

$connectionName = "AzureRunAsConnection"
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
$null = Add-AzureRmAccount -ServicePrincipal -TenantId $servicePrincipalConnection.TenantId -ApplicationId $servicePrincipalConnection.ApplicationId -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
$VMs = Get-AzureRmResource|Where-Object {$_.Tags.Keys -eq "owner" -and $_.Tags.Values -eq "daneum"}
foreach ($VM in $VMs) {
    if ($VM.ResourceType -eq "Microsoft.Compute/virtualMachines") {
        Start-AzureRmVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -Verbose
    }
}

停止虚拟机

$connectionName = "AzureRunAsConnection"
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
$null = Add-AzureRmAccount -ServicePrincipal -TenantId $servicePrincipalConnection.TenantId -ApplicationId $servicePrincipalConnection.ApplicationId -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
$VMs = Get-AzureRmResource|Where-Object {$_.Tags.Keys -eq "owner" -and $_.Tags.Values -eq "daneum"}
foreach ($VM in $VMs) {
    if ($VM.ResourceType -eq "Microsoft.Compute/virtualMachines") {
        Stop-AzureRmVM -ResourceGroupName $VM.ResourceGroupName -Name $VM.Name -Force -Verbose
    }
}

对于 webhook 集成过程,您可以查看 here

关于通过 webhook Azure 启动/停止 VM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55813013/

相关文章:

azure - 发布到 Azure Blob 存储时自动生成缩略图或预览图像

Azure SQL 无服务器数据库 CPU 不断计费

azure - 删除多个 Azure 数据工厂管道

powershell - Azure-runbook 中的 New-PSSession (ARM)

powershell - Azure 存储 - 生成 SAS token (门户与 PowerShell

azure - 部署arm模板前检查Azure VM名称是否存在

Azure 通知中心 : How to view per message telemetry?

azure - 如何显示 Azure 的用户 ID?

azure - 使用突触分析管道将 Blob 文件数据插入现有 SQL 表的预复制脚本

c# - 从辅助角色使用 Azure Cmdlet