首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >获取Azure资源组创建时间

获取Azure资源组创建时间
EN

Stack Overflow用户
提问于 2018-06-01 23:17:25
回答 2查看 5.7K关注 0票数 6

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

不幸的是,资源组上没有创建时间戳属性。使用Get-AzureRmResourceGroup返回的对象如下:

代码语言:javascript
运行
复制
ResourceGroupName : eastus2-something-rg
Location          : eastus2
ProvisioningState : Succeeded
Tags              :
ResourceId        : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/eastus2-something-rg

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

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

EN

回答 2

Stack Overflow用户

发布于 2018-06-01 23:17:25

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

但是管理操作记录在日志中,并且可以使用Get-AzureRmLog命令检索这些日志。

下面是一条PowerShell语句,它遍历订阅的资源组,并查找n天或更多天前(从this gist)创建的资源组:

代码语言:javascript
运行
复制
$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

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

票数 5
EN

Stack Overflow用户

发布于 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的添加到查询参数中,即:

代码语言:javascript
运行
复制
GET https://management.azure.com/subscriptions/1237f4d2-3dce-4b96-ad95-677f764e7123/resourcegroups?api-version=2019-08-01&%24expand=createdTime
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50646547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档