gpt4 book ai didi

powershell - 如何使用 PowerShell 获取 Azure 中运行的 VM 脚本的 ResourceId?

转载 作者:行者123 更新时间:2023-12-02 23:55:30 25 4
gpt4 key购买 nike

我正在编写一个脚本来在 Azure 中设置警报,我想通过 Azure 中的自定义脚本扩展运行该脚本,但运行 Add-AzureRmMetricAlertRule 所需的参数之一仍然是 TargetResourceId 这是应配置警报的虚拟机的 ResourceId。

所以现在我想知道 - 如何使用 PowerShell 获取当前虚拟机的 ResourceId?

我尝试的所有操作都假设我有一个 ResourceName 或者我迭代订阅中的虚拟机列表,而我感兴趣的是运行脚本的特定实例。

最佳答案

Azure 有一个 instance metadata service与 AWS/GCE 提​​供的类似。您应该能够执行以下操作:

curl :

curl -H Metadata:true "http://169.254.169.254/metadata/instance?api-version=2019-11-01"

PowerShell:

(Invoke-RestMethod "http://169.254.169.254/metadata/instance?api-version=2019-11-01" -Headers @{Metadata = $true})

从服务器获取实例元数据。

请注意,在上面的 PowerShell 示例中,Invoke-RestMethod 会自动将 JSON 响应转换为 PowerShell 对象类型。

此功能在此请求/讨论:https://feedback.azure.com/forums/216843-virtual-machines/suggestions/6204911-provide-virtual-machine-instance-metadata-support

关于powershell - 如何使用 PowerShell 获取 Azure 中运行的 VM 脚本的 ResourceId?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45824329/

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