【问题标题】:Get Azure resource group creation time获取 Azure 资源组创建时间
【发布时间】:2018-06-01 15:17:24
【问题描述】:

意图:了解第一次创建资源组的时间。客户组织想要报告资源组创建时间戳并采取行动。这将在自动化脚本中使用。

很遗憾,资源组上没有 创建时间戳 属性。使用Get-AzureRmResourceGroup 会返回如下对象:

ResourceGroupName : eastus2-something-rg
Location          : eastus2
ProvisioningState : Succeeded
Tags              :
ResourceId        : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/eastus2-something-rg

此功能有been requested,但似乎没有多少票。

如何检索资源组的创建时间戳?

【问题讨论】:

    标签: azure azure-rm


    【解决方案1】:

    确实,资源组没有创建时间戳。

    但管理操作记录在日志中,这些日志可以通过Get-AzureRmLog命令检索。

    这是一个 PowerShell 语句,它遍历订阅的资源组并查找 n 天或更早前创建的资源组(来自 this gist):

    $days = 7
    $pointInTime = [DateTime]::Now.AddDays(-$days);
    $horizon = $pointInTime.AddDays(-$days);
    
    "===Removing resource groups created between $horizon and $pointInTime==="
    
    # Get potential log entries
    $logs = @()
    $logs += Get-AzureRmLog -StartTime $horizon -EndTime $pointInTime -Status "Succeeded" -ResourceProvider "Microsoft.Resources" -WarningAction "SilentlyContinue" `
        | Select-Object ResourceGroupName, ResourceId, @{Name="EventNameValue"; Expression={$_.EventName.Value}}, @{Name="OperationNameValue"; Expression={$_.OperationName.Value}}, EventTimestamp, @{Name="HttpVerb"; Expression={$_.HttpRequest.Method}} `
        | Where-Object -FilterScript {$_.EventNameValue -EQ "EndRequest" -and $_.OperationNameValue -eq "Microsoft.Resources/subscriptions/resourcegroups/write" -and $_.HttpVerb -eq "PUT"} `
        | Select-Object -ExpandProperty ResourceGroupName -Unique
    
    "Expired resource groups (created BEFORE $pointInTime) -> $logs"
    
    # Get recent log entries to remove from the list
    $nologs = @()
    $nologs += Get-AzureRmLog -StartTime $pointInTime -Status "Succeeded" -ResourceProvider "Microsoft.Resources" -WarningAction "SilentlyContinue" `
    | Select-Object ResourceGroupName, ResourceId, @{Name="EventNameValue"; Expression={$_.EventName.Value}}, @{Name="OperationNameValue"; Expression={$_.OperationName.Value}}, EventTimestamp, @{Name="HttpVerb"; Expression={$_.HttpRequest.Method}} `
    | Where-Object -FilterScript {$_.EventNameValue -EQ "EndRequest" -and $_.OperationNameValue -eq "Microsoft.Resources/subscriptions/resourcegroups/write" -and $_.HttpVerb -eq "PUT"} `
    | Select-Object -ExpandProperty ResourceGroupName -Unique
    
    "Resource groups created AFTER $pointInTime -> $nologs"
    
    # remove any that were found to have recent creation
    $rgs = $logs | Where-Object {$nologs -notcontains $_} | Select-Object @{Name="ResourceGroupName"; Expression={$_}} | Get-AzureRmResourceGroup -ErrorAction "SilentlyContinue"
    
    "Existing resource groups to delete -> $($rgs | Select-Object -ExpandProperty ResourceGroupName)"
    
    $rgs | Remove-AzureRmResourceGroup -Force -AsJob
    

    它返回正在运行以删除资源组的作业列表(它们可能需要一些时间,具体取决于它们的内容)。

    【讨论】:

    • Azure 审核日志仅包含 90 天的数据。有什么方法可以获取去年某个时间创建的 RG 的数据(例如 RG 创建时间)??
    • 您可以查询部署。 RG 不会记录在部署日志中,但您可能可以使用最早部署到 RG 的资源。此外,部署可以被删除,所以你也有风险。
    【解决方案2】:

    此信息可通过 ARM 获得,但您必须直接调用 API 而不是 PS Get-AzureRmResourceGroup(或 Get-AzResourceGroup)cmdlet。

    Deleting all resources in an Azure Resource Group with age more than x days

    本质上,您需要将 $expand=createdTime 添加到您的查询参数中,即:

    GET https://management.azure.com/subscriptions/1237f4d2-3dce-4b96-ad95-677f764e7123/resourcegroups?api-version=2019-08-01&%24expand=createdTime
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-18
      • 2022-07-14
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 2018-02-28
      • 1970-01-01
      相关资源
      最近更新 更多