Azure:如何比较策略中的日期

Azure: how to compare dates in a policy

提问人:samauces 提问时间:10/27/2023 更新时间:10/27/2023 访问量:38

问:

我正在尝试在 Azure 中实施一项策略,以便在 15 天后删除快照,为此我需要比较日期,我正在使用标记,但我的策略未正确配置,因为正在评估的资源被标记为合规,当两个资源标签具有不同的日期时,1 应标记为不合规, 我想我没有像我应该的那样比较日期

这是我的政策定义

{
  "properties": {
    "displayName": "snapshot-deletion-test",
    "policyType": "Custom",
    "mode": "All",
    "description": "this is a policy which will delete all the snapshots older than 15 days, this is the first test, only checks the snapshots on the HCE-general-RG resource group which is under hce-chq-sandbox-nonprod sub",
    "metadata": {
      "createdBy": "ce5c7636-f9a1-462a-9c8f-7ff7f679d813",
      "createdOn": "2023-10-23T22:17:16.7301903Z",
      "updatedBy": "c...",
      "updatedOn": "2023-10-25T18:56:07.1594354Z"
    },
    "parameters": {},
    "policyRule": {
      "if": {
        "allof": [
          {
            "field": "type",
            "equals": "microsoft.compute/snapshots"
          },
          {
            "field": "tags['timeCreated']",
            "greaterOrEquals": "addDays(utcNow(),1)"
          }
        ]
      },
      "then": {
        "effect": "audit"
      }
    }
  },
  "id": "/...",
  "type": "Microsoft.Authorization/policyDefinitions",
  "name": "...",
  "systemData": {
    "createdBy": "Samuel....",
    "createdByType": "User",
    "createdAt": "2023-10-23T22:17:16.6801294Z",
    "lastModifiedBy": "Samuel....",
    "lastModifiedByType": "User",
    "lastModifiedAt": "2023-10-25T18:56:07.0443867Z"
  }
}

如您所见,策略规则具有此评估 “greaterOrEquals”: “addDays(utcNow(),1)”,它应该检查日期和标签 timeCreated 是否等于今天或未来 1 天,将被标记为合规

这是正在评估的 2 个资源中的 1 个,标记为 trueenter image description here

也许我没有与正确的日期格式进行比较,或者是策略中的其他内容。

任何帮助都非常感谢

Azure 标记 策略

评论


答: 暂无答案