0
votes

Is there a way to enable the Auto-start feature on a DevTest Labs virtual machine as part of the creation i.e. can it be added to the VM's ARM template?

I enable this manually via the Azure portal currently, but I'm finding that it gets disabled when subsequent deployments from Team Services are made.

Solution

Inspired by the accepted answer from Ashok below, I've managed to tweak and simplify the PowerShell script to the following...

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
1
How do you subsequent deployments from Team Services? - starian chen-MSFT
As part of the release definition in Team Services, we use the DevTest Labs Create VM task to ensure the machine is available before deployemnt. I can only assume this process is what is causing the machines to lose their auto-start tag. Maybe due to the ARM template supplied during VM creation? Perhaps there's another way to handle this such as the PowerShell in Ashok's answer below. - smd
Could you get necessary information through Get-AzureRmResource command? $VmResourceId = "subscriptions/$subscriptionId/resourcegroups/$labResourceGroup/providers/microsoft.devtestlab/labs/$labName/virtualmachines/$VmName" $vm = Get-AzureRmResource -ResourceId $VmResourceId -ExpandProperties - starian chen-MSFT

1 Answers

2
votes

The auto-start policy requires you to explicitly select a VM and apply the policy from its context menu after you enable the policy. That’s way you won't easily run into the situation where unwanted VMs are accidentally auto-started and cause unexpected spending.

For more details, refer to the following article:

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

Update:

You can try the following PS function. Note that the tags collection must be replaced in whole. That's why you see logic that makes sure to only append to the collection, or change the existing values, if already existing. Otherwise, you will be removing other tags.

    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
}