powershell - 使用自动化帐户清除 Set-AzureRmTag 中的标签

标签 powershell azure

我想使用自动化脚本更改标签上的值。用户将有一个启动脚本,它将关闭标签键从 true 更改为 false。

当我使用下面的脚本单独设置标签时,它将标签值设置为 false。当前设置为 true。

当我使用自动化脚本时,它会删除​​所有标签,但是,如果我在脚本中指定虚拟机,自动帐户就会工作并将键值从 false 更改为 true。

我看不到我错过了什么。这是来自 webhook 并作为 powershell 脚本运行,而不是工作流程。

[CmdletBinding()]
    Param(
        [Parameter(Mandatory=$True)]
        [object]$WebhookData            
    )

    Write-Output "------------------------------------------------"
    Write-Output "`nConnecting to Azure Automation"

    $Connection = Get-AutomationConnection -Name AzureRunAsConnection
    Add-AzureRMAccount -ServicePrincipal -Tenant $Connection.TenantID `
    -ApplicationId $Connection.ApplicationID -CertificateThumbprint $Connection.CertificateThumbprint

    $RunbookVersion = "0.0.17"
    $timeStartUTC     = (Get-Date).ToUniversalTime()

    Write-Output "Workflow started: Runbook Version is $RunbookVersion"
    Write-Output "System time is: $(Get-Date)"

    Write-Output "`nGetting tagged resources"
    Write-Output "------------------------------------------------"

    $ResourceGroupFilter  = ""

    $SupportedEnvironments = "DEV, Test, PREProd, Prod"

    $isWebhookDataNull = $WebhookData -eq $null
    Write-Output "Is webhook data null ? :  $($isWebhookDataNull)"

      # If runbook was called from Webhook, WebhookData will not be null.
    If ($WebhookData -ne $null) {
        # Collect properties of WebhookData
        $WebhookName     =     $WebhookData.WebhookName
        $WebhookHeaders  =     $WebhookData.RequestHeader
        $WebhookBody     =     $WebhookData.RequestBody

        $body = $WebhookBody | ConvertFrom-Json

        $UserEmail = $body.user.email
        Write-Output "Runbook started from webhook '$WebhookName' by '$($body.user.email)' for environment '$($body.environment)'"
        Write-Output "Message body: " $WebhookBody

    }
    else {
        Write-Error "Runbook mean to be started only from webhook."
    }

    If ($body.environment.ToUpper() -eq 'DEV') {       
        $ResourceGroupFilter = 'The-DEV-RG'
    }
    if ($ResourceGroupFilter -eq "") {
     Exit 1
    }
    if($VMRG -eq ''){
        Write-Output "No resource groups matched for selected environment. Webhook cant progress further, exiting.."
        Write-Error "No resource groups matched for selected environment. Webhook cant progress further, exiting.."
        Exit 1
    }   
            $rgs = Get-AzureRmResourceGroup | Where-Object {$_.ResourceGroupName -like "*$rg*"} 
        foreach ($rg in $rgs) 
        {
        $vms = Get-AzureRmVm -ResourceGroupName $rg.ResourceGroupName
            $vms.ForEach({
                $tags = $_.Tags
                $tags['ShutdownSchedule_AllowStop'] = "$False";
                Set-AzureRmResource -ResourceId $_.Id -Tag $tags -Force -Verbose
        })
        }
    ForEach ($vm in $vms) {
        Start-AzureRmVM -Name $vm.Name -ResourceGroupName $vm.ResourceGroupName -Verbose
    }

提前致谢:)

最佳答案

根本原因是您本地的 Azure Power Shell 是最新版本,但在 Azure 自动化帐户中,它不是最新版本。我在实验室测试过,旧版本不支持此功能。

您需要升级 Azure Power Shell 版本。有关此的更多信息请参阅此 answer .

关于powershell - 使用自动化帐户清除 Set-AzureRmTag 中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48621479/

相关文章:

c# - 使用 C# 中的 CosmosDB 和 Microsoft.Azure.Cosmos 在 Azure Functions 上实现单例

javascript - 单个 React 应用程序可以在多个不同的 URL 上提供服务吗

arrays - 如何按每个新行拆分字符串并将每一行放入 PowerShell 中的单独数组条目中

powershell - 创建 Azure SQL 数据库服务器连接上下文 New-AzureSqlDatabaseServerContext

powershell - Powershell在搜索字符串后返回下一个单词

azure - 应用程序 channel 和机器人 channel 有什么区别?

azure api 管理器 terraform 查询

python - 无法在 Azure 机器学习工作室中打开新的 Python 或 R 笔记本

regex - 使用Powershell的正则表达式

azure - 想要在一个脚本中获取 blob 存储的所有详细信息