You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过API或PowerShell检索已删除Azure VM的VMID?

检索已删除Azure虚拟机的VMID

因为变更分析API依赖ARM模板存在,VM删除后无法通过该接口获取信息,你可以通过活动日志来程序化提取已删除VM的VMID,以下是具体实现方法:

PowerShell 实现

使用Get-AzLog命令过滤删除虚拟机的操作事件,从事件属性中提取VMID:

# 替换为你的资源组名称
$resourceGroupName = "your-resource-group-name"
# 可根据需要调整时间范围,活动日志默认保留90天
$startTime = (Get-Date).AddDays(-30)

# 获取成功删除VM的活动日志事件
$deleteVmEvents = Get-AzLog -ResourceGroupName $resourceGroupName -StartTime $startTime | 
    Where-Object {
        $_.OperationName.Value -eq "Microsoft.Compute/virtualMachines/delete" -and 
        $_.Status.Value -eq "Succeeded"
    }

# 遍历事件提取VMID
foreach ($event in $deleteVmEvents) {
    # 解析事件响应中的VMID
    $vmDetails = $event.Properties.response | ConvertFrom-Json
    Write-Host "已删除VM名称: $($event.TargetResourceName) | VMID: $($vmDetails.vmId)"
}

REST API 实现

调用Azure Monitor活动日志API,筛选删除VM的操作,从返回结果中提取VMID:

请求示例

GET https://management.azure.com/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/microsoft.insights/eventtypes/management/values?api-version=2015-04-01&$filter=eventTimestamp ge '2024-01-01T00:00:00Z' and eventTimestamp le '2024-02-01T00:00:00Z' and operationName eq 'Microsoft.Compute/virtualMachines/delete' and status eq 'Succeeded'

提取VMID

在返回的JSON数据中,每个事件的properties.response字段是VM删除操作的响应内容,解析后即可获取vmId字段值。

内容的提问来源于stack exchange,提问作者Ajith

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 00:53:24