哈希表未在Azure自动化中读取为哈希表 [英] Hashtable not being read as hashtable in Azure Automation

查看:51
本文介绍了哈希表未在Azure自动化中读取为哈希表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一段代码可以在Powershell ISE中完美地运行,但是,当我们在Azure自动化中使用相同的代码时,它将清除标记.

We have a piece of code which works perfectly in Powershell ISE, however when we use the same code in Azure automation, it clears the tags.

    $rgs = Get-AzureRmResourceGroup | Where-Object {$_.ResourceGroupName -like "*$rg*"} 
foreach ($rg in $rgs) 
{
$vms = Get-AzureRmVm -ResourceGroupName $rg.ResourceGroupName
    $vms.ForEach({
        $tags = $vm.Tags
        $tags['ShutdownSchedule_AllowStop'] = "$False";
        Set-AzureRmResource -ResourceId $_.Id -Tag $tags -Force -Verbose
})
}

到目前为止,我设法建立的代码是 Set-AzureRmResource -ResourceId $ _.Id -Tag $ tags -Force -Verbose 不会将$ tags视为hastable.我已经进行了一些调试,可以看到 $ tags = $ vm.Tags 是一个哈希表.

What I've managed to establish so far is that the code Set-AzureRmResource -ResourceId $_.Id -Tag $tags -Force -Verbose does not see $tags as a hastable. I've done some debugging and I can see $tags = $vm.Tags is a hashtable.

我在google上四处浏览,并且看到了一些使用 [system.collections.hashtable] 来指定它是哈希表的提法,但这是我的能力受到限制的地方.我希望有人可以指出正确的方向.

I looked around on google and I've seen a few mentions of using [system.collections.hashtable] to specify it is a hashtable, but this is where my powershell is limited. I was hoping someone could point me in the right direction.

我尝试做 ResourceId $ _.Id -Tag [system.collections.hashtable] $ tags ,但这没用.

我们试图做的是将标记值从true更改为false.密钥为Shutdown_AllowStop,当前值设置为True.

What we are trying to do is change a tag value from true to false. The Key is Shutdown_AllowStop and the value is currently set to True.

先谢谢了(真的很希望这样):)

Thanks in advance (and really hoping this makes sense) :)

推荐答案

以下脚本对我有用.

$rgs = Get-AzureRmResourceGroup | Where-Object {$_.ResourceGroupName -like *$rg*"} 

foreach ($rg in $rgs) 
{
$vms = Get-AzureRmVm -ResourceGroupName $rg.ResourceGroupName
    foreach ($vm in $vms)
    {
        $tags = $vm.Tags
        foreach ($tag in $tags)
        {

            $tag['ShutdownSchedule_AllowStop'] = "$True";
            Write-Output ("Showing VM's resource ID " + $vm.ID)
            Write-output ("Show VM's tag "+[System.Collections.Hashtable]::new($tag))
            $hash = [System.Collections.Hashtable]::new($tag)
            $hash['ShutdownSchedule_AllowStop']
            Set-AzureRmResource -ResourceId $vm.ID -Tag $hash  -ApiVersion "2017-12-01" -Force -Verbose
        }

    }
}

但是您需要在自动化帐户中更新Azure Power Shell版本,如果不执行此操作,则脚本将不起作用.

But you need update Azure Power Shell version in Automation Account, if you don't do this, the script does not work.

这是我的测试结果.

这篇关于哈希表未在Azure自动化中读取为哈希表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆