0

有没有办法在开发测试实验室虚拟机上启用自动启动功能作为创建的一部分,即它可以添加到 VM 的 ARM 模板中吗?

我目前通过 Azure 门户手动启用此功能,但我发现从 Team Services 进行后续部署时它会被禁用。

解决方案

受下面 Ashok 接受的答案的启发,我设法将 PowerShell 脚本调整并简化为以下内容......

Param([string] $resourceId)

$tags = (Get-AzureRmResource -ResourceId $resourceId).Tags

if (-Not ($tags) -Or -Not($tags.ContainsKey('AutoStartOn'))) {
  $tags += @{ AutoStartOn=$true; }
}

if (-Not ($tags) -Or -Not($tags.ContainsKey('AlwaysOn'))) {
  $tags += @{ AlwaysOn=$true; }
}

Set-AzureRmResource -ResourceId $resourceId -Tag $tags -Force
4

1 回答 1

2

自动启动策略要求您在启用策略后明确选择 VM 并从其上下文菜单中应用该策略。这样您就不会轻易遇到不需要的虚拟机意外自动启动并导致意外支出的情况。

有关详细信息,请参阅以下文章:

https://azure.microsoft.com/en-us/updates/azure-devtest-labs-schedule-vm-auto-start/

更新:

你可以试试下面的PS功能。请注意,标签集合必须全部替换。这就是为什么您会看到确保仅附加到集合或更改现有值(如果已经存在)的逻辑。否则,您将删除其他标签。

    function Enable-AzureDtlVmAutoStart
{
    [CmdletBinding()]
    param(
        [string] $ResourceId,
        [switch] $AlwaysOn
    )

    $autoStartOnTagName = 'AutoStartOn'
    $alwaysOnTagName = 'AlwaysOn'

    $labVm = Get-AzureRmResource -ResourceId $ResourceId
    $tags = $labVm.Tags

    # Undefined tags collection can happen if the Lab VM never had any tags set.
    if (-not $tags)
    {
        $tags = @(@{},@{})
    }

    # Update the tags if they already exist in the collection.
    $tags | % {
        if ($_.Name -eq $autoStartOnTagName)
        {
            $_.Value = $true
        }
        if ($_.Name -eq $alwaysOnTagName)
        {
            $_.Value = $true
        }
    }
    # Otherwise, create new tags.
    if (-not ($tags | ? { $_.Name -eq $autoStartOnTagName }))
    {
        $tags += @{Name=$autoStartOnTagName;Value=$true}
    }
    if (-not ($tags | ? { $_.Name -eq $alwaysOn }))
    {
        $tags += @{Name=$alwaysOnTagName;Value=$AlwaysOn}
    }

    Set-AzureRmResource -ResourceId $ResourceId -Tag $tags -Force
}
于 2017-10-17T08:25:32.300 回答