可以将格式参数传递给uctnow()函数

发布于 2025-02-13 21:39:02 字数 2414 浏览 0 评论 0原文

我尝试创建一个Azure策略,该策略在新创建的资源上附加了创建的:DD/mm/yyyy 标签。

我正在使用以下默认策略:

{
  "properties": {
    "displayName": "Append a tag and its value to resources",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Appends the specified tag and value when any resource which is missing this tag is created or updated. Does not modify the tags of resources created before this policy was applied until those resources are changed. Does not apply to resource groups. New 'modify' effect policies are available that support remediation of tags on existing resources (see https://aka.ms/modifydoc).",
    "metadata": {
      "version": "1.0.1",
      "category": "Tags"
    },
    "parameters": {
      "tagName": {
        "type": "String",
        "metadata": {
          "displayName": "Tag Name",
          "description": "Name of the tag, such as 'environment'"
        }
      },
      "tagValue": {
        "type": "String",
        "metadata": {
          "displayName": "Tag Value",
          "description": "Value of the tag, such as 'production'"
        }
      }
    },
    "policyRule": {
      "if": {
        "field": "[concat('tags[', parameters('tagName'), ']')]",
        "exists": "false"
      },
      "then": {
        "effect": "append",
        "details": [
          {
            "field": "[concat('tags[', parameters('tagName'), ']')]",
            "value": "[parameters('tagValue')]"
          }
        ]
      }
    }
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/2a0e14a6-b0a6-4fab-991a-187a4f81c498",
  "type": "Microsoft.Authorization/policyDefinitions",
  "name": "2a0e14a6-b0a6-4fab-991a-187a4f81c498"
}

具有以下参数: [utcnow('d')]

”输入图像描述在这里“

不幸的是,您可以看到我不断收到此错误消息。

内部异常“策略语言函数'utcnow'具有“ 1” 参数。预期的参数数为“ 0”。

a>,我不应该将'd'参数设置为该功能吗?

如果我删除了有效的参数,并根据文档所说,在yyyymmddthhmmssz格式中给我日期。

如何改用dd/mm/yyyy格式获得日期?

I try to create an azure policy that appends a created-on : dd/mm/yyyy tag on newly created resources.

I'm using the following default policy :

{
  "properties": {
    "displayName": "Append a tag and its value to resources",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Appends the specified tag and value when any resource which is missing this tag is created or updated. Does not modify the tags of resources created before this policy was applied until those resources are changed. Does not apply to resource groups. New 'modify' effect policies are available that support remediation of tags on existing resources (see https://aka.ms/modifydoc).",
    "metadata": {
      "version": "1.0.1",
      "category": "Tags"
    },
    "parameters": {
      "tagName": {
        "type": "String",
        "metadata": {
          "displayName": "Tag Name",
          "description": "Name of the tag, such as 'environment'"
        }
      },
      "tagValue": {
        "type": "String",
        "metadata": {
          "displayName": "Tag Value",
          "description": "Value of the tag, such as 'production'"
        }
      }
    },
    "policyRule": {
      "if": {
        "field": "[concat('tags[', parameters('tagName'), ']')]",
        "exists": "false"
      },
      "then": {
        "effect": "append",
        "details": [
          {
            "field": "[concat('tags[', parameters('tagName'), ']')]",
            "value": "[parameters('tagValue')]"
          }
        ]
      }
    }
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/2a0e14a6-b0a6-4fab-991a-187a4f81c498",
  "type": "Microsoft.Authorization/policyDefinitions",
  "name": "2a0e14a6-b0a6-4fab-991a-187a4f81c498"
}

With the following parameters : [utcNow('d')]

enter image description here

Unfortunately as you can see I keep getting this error message.

The inner exception 'The policy language function 'utcNow' has '1'
argument(s). Expected number of arguments is '0'.

According to the documentation, shouldn't I be able to set a 'd' parameters to the function ?

If I removed the parameters it works and gives me the date in yyyyMMddTHHmmssZ format as per the documentation says.

How to get the date in dd/mm/yyyy format instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心舞飞扬 2025-02-20 21:39:02

某些ARM模板功能不允许在策略定义中使用。您可以检查 >。

UTCNOW() - 与ARM模板不同,该属性可以在默认值外部使用。
返回一个设置为通用ISO 8601 DateTime格式的当前日期和时间的字符串YYYY-MM-DDTHH:MM:SS.FFFFFFFZ。

Some of the ARM template functions are not allowed to use in policy definition. You can check it here.

utcNow() - Unlike an ARM template, this property can be used outside defaultValue.
Returns a string that is set to the current date and time in Universal ISO 8601 DateTime format yyyy-MM-ddTHH:mm:ss.fffffffZ.

感性不性感 2025-02-20 21:39:02

我看到的最好的解决方案是利用Concat功能以及子字符串。我的实施看起来像这样:

"field": "tags['CreatedOnDate']",
            "value": "[concat(substring(utcNow(),5,2),'/', substring(utcNow(),8,2),'/',substring(utcNow(),0,4),'-',substring(utcNow(),11,8))]"

产生11/30/2022-18:45:55的结果

The best solution I have seen makes use of the Concat function along with substring. My implementation looks like this:

"field": "tags['CreatedOnDate']",
            "value": "[concat(substring(utcNow(),5,2),'/', substring(utcNow(),8,2),'/',substring(utcNow(),0,4),'-',substring(utcNow(),11,8))]"

Which produces a result like 11/30/2022-18:45:55

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文