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

时间:2018-02-05 11:21:41

标签: powershell azure

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

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

当我使用自动化脚本时,它会擦除​​所有标记,但是如果我在脚本中指定了vm,则自动机帐户可以工作,并将键值从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
    }

提前致谢:)

1 个答案:

答案 0 :(得分:1)

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

您需要升级Azure Power Shell版本。有关此问题的详细信息,请参阅此answer