Intent:知道第一次创建资源组的时间。客户端组织希望报告资源组创建时间戳并对其执行操作。这将在自动化脚本中使用。
不幸的是,资源组上没有创建时间戳属性。使用Get-AzureRmResourceGroup返回的对象如下:
ResourceGroupName : eastus2-something-rg
Location : eastus2
ProvisioningState : Succeeded
Tags :
ResourceId : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/eastus2-something-rg此功能有been requested,但似乎没有太多投票。
如何检索资源组的创建时间戳?
发布于 2018-06-01 23:17:25
实际上,资源组没有创建时间戳。
但是管理操作记录在日志中,并且可以使用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它返回正在运行以删除资源组的作业列表(根据其内容,这些作业可能需要一些时间)。
发布于 2020-10-13 05:20:30
此信息可通过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=createdTimehttps://stackoverflow.com/questions/50646547
复制相似问题