【问题标题】:DevTest Labs Virtual Machine Auto-start开发测试实验室虚拟机自动启动
【发布时间】:2017-10-17 08:02:21
【问题描述】:

有没有办法在开发测试实验室虚拟机上启用自动启动功能作为创建的一部分,即可以将其添加到 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

【问题讨论】:

  • 您如何从 Team Services 进行后续部署?
  • 作为 Team Services 中发布定义的一部分,我们使用 DevTest Labs Create VM 任务来确保机器在部署之前可用。我只能假设这个过程是导致机器丢失其 auto-start 标记的原因。可能是由于在 VM 创建期间提供的 ARM 模板?也许还有另一种方法来处理这个问题,比如下面 Ashok 的回答中的 PowerShell。
  • 能否通过 Get-AzureRmResource 命令获取必要的信息? $VmResourceId = "subscriptions/$subscriptionId/resourcegroups/$labResourceGroup/providers/microsoft.devtestlab/labs/$labName/virtualmachines/$VmName" $vm = Get-AzureRmResource -ResourceId $VmResourceId -ExpandProperties

标签: azure-devops azure-virtual-machine azure-resource-manager azure-pipelines-release-pipeline azure-devtest-labs


【解决方案1】:

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

更多详情,请参考以下文章:

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
}

【讨论】:

  • 感谢您的回复。当我在网上寻找可能的解决方案时(在提交这个 SO 问题之前),我遇到了上面的 PS 方法——这很好——我可以更进一步并采用该解决方案。对我来说仍然很奇怪,即使尚未重新创建服务器,自动启动也会被禁用,我应该自己管理它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多