Azure 资源使用 PowerShell 自动标记创建者和创建日期

问题描述 投票:0回答:1

如何自动添加标签:CreatedBy和createdDate

这是我正在尝试做的事情

这个想法是创建一个预定的运行手册。它将每 10 运行一次 分钟。这将获取所有未标记的资源并应用 为订阅中的所有资源添加“CreatedBy”和“CreatedDate”标签。

我依赖

Activity Logs and  Automation Account

这就是我所做的。

$Appid = "Appid"
$PSWord = ConvertTo-SecureString -String "APPSecret" -AsPlainText -Force
$tenantID = "tenantID"
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $Appid,$PSWord
Connect-AzAccount -Credential $Credential -Tenant   $tenantID -ServicePrincipal -Subscription "SubID"
Set-AzContext -Tenant $tenantID
$resources = Get-AzResource

$currentTime = Get-Date
$endTime = $currentTime.AddDays(-7 * $cnt)
$startTime = $endTime.AddDays(-7)

$resources.foreach{
    $resource = $_
    $untaggedResources = $resource.Tags["createdBy","createdTime"]
    if($untaggedResources -eq $null)
    {
        $creationTime = (Get-AzResourceGroup -Name $resource.ResourceGroupName).Resources |
                        Where-Object { $_.ResourceId -eq $resource.ResourceId } |
                        Select-Object -ExpandProperty Properties |
                        Select-Object -ExpandProperty CreatedTime

        $owner = Get-AzLog -ResourceId $resource.ResourceId -StartTime $startTime -EndTime $endTime |
                 Where-Object {$_.Authorization.Action -like "*/write*"} |
                 Select-Object -ExpandProperty Caller |
                 Group-Object |
                 Sort-Object |
                 Select-Object -ExpandProperty Name

        $resource.Tags.Add("createdBy", $owner)
        $resource.Tags.Add("createdTime", $creationTime)
        $resource | Set-AzResource -Force
    }
}

我已经使用此脚本创建了一个 Runbook,该脚本已完成,没有任何错误。然而它并没有按预期工作。

首先它不会更新资源中的createdTime。它将添加标签名称作为createdTime,但值只是显示空白。

其次,createdBy 正在更新,但是随着用户电子邮件的一起,它也在字段中添加了额外的 id。

有什么可能的方法来实现这一点吗?

azure powershell azure-cli azure-automation
1个回答
0
投票

当我在我的环境中尝试使用您提供的相同代码时,我总是收到

$owner
$Creationtime
结果为空。这意味着这些变量不包含与资源相关的信息。

为了修复它,请使用

-ExpandProperties
Get-AzResource
命令,详细信息如下。

首先,要使用 PowerShell 获取

CreatedTime
,请使用:

$resources=(Get-Azresource -ExpandProperties).Properties

enter image description here

修改代码以满足您的要求。

$resources = Get-Azresource
foreach ($resource in $resources) {
      $tags = $resource.Tags
  if (-not $tags.ContainsKey("createdBy") -and -not $tags.ContainsKey("createdTime")) {          
          $property = (Get-Azresource -ExpandProperties).Properties 
  $CreatedTime = $property.CreationTime
  
          $endTime = (Get-Date).AddDays(-7)
          $startTime = $endTime.AddDays(-7)
  $owner = Get-AzLog -ResourceId $resource.ResourceId -StartTime $startTime -EndTime $endTime |
                   Where-Object { $_.Authorization.Action -like "*/write*" } |
                   Select-Object -ExpandProperty Caller |
                   Group-Object |
                   Sort-Object |
                   Select-Object -ExpandProperty Name
 
 $modifiedTags = @{
             "createdBy" = $owner
             "creationTime" = $CreatedTime
         }
  
         $resource | Set-AzResource -Tag $modifiedTags -Force
      }
  }
© www.soinside.com 2019 - 2024. All rights reserved.