获取Azure资源组创建时间

epfja78i  于 2023-02-05  发布在  其他
关注(0)|答案(4)|浏览(129)

目的:了解首次创建资源组的时间。客户端组织希望报告资源组创建时间戳并对其执行操作。这将在自动化脚本中使用。

遗憾的是,资源组上没有 creation timestamp 属性。使用Get-AzureRmResourceGroup将返回如下对象:

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

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

ca1c2owp

ca1c2owp1#

实际上,资源组没有创建时间戳。
但是管理操作记录在日志中,可以使用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

它返回正在运行以删除资源组的作业列表(根据其内容,可能需要一些时间)。

rdlzhqv9

rdlzhqv92#

使用以下PowerShell cmdlet获取Azure资源组的创建日期和时间(AzRestMethod)

$subId = (Get-AzContext).Subscription.ID
((Invoke-AzRestMethod -Path "/subscriptions/$subId/resourcegroups?api-version=2020-06-01&`$expand=createdTime" -Method GET).Content|ConvertFrom-Json).value|select name,createdTime
t5zmwmid

t5zmwmid3#

此信息可通过ARM获得,但您必须直接调用API,而不是PS Get-AzureRmResourceGroup(或Get-AzResourceGroup)cmdlet。
请参阅删除Azure资源组中存在时间超过x天的所有资源
实际上,您需要将$expand=createdTime添加到查询参数中,即:

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

332nm8kg4#

您可以使用az group deployment list -g [RESOURCE_GROUP_NAME],然后使用任何您喜欢的方法解析出最旧的“timestamp”值。
请注意,在撰写本文时,az group deployment已被隐式弃用,将被az deployment group取代。

相关问题