gpt4 book ai didi

azure - 获取 Azure 资源组创建时间

转载 作者:行者123 更新时间:2023-12-04 19:27:52 27 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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

但管理操作会记录在日志中,这些日志可以通过 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 - 获取 Azure 资源组创建时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50646547/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com