Skip to content

Tags are no longer hashtable array #8

@shin-akuma

Description

@shin-akuma

Hi Paul,
Thanks for sharing your brilliant idea on the schedule tag. I'm using it in our Azure Sub to saving cost. However, I think you script was developed when the tags property of AzureRMReource is still hashtable array. At the moment, the tags property is just hashtable. Therefore, a few tweaks need to be made to your scripts:

function ContainsSchedule
{
	param
	(
		[Parameter(Mandatory=$false)]
		[System.Collections.Hashtable]$tags
	)

	if ($tags -eq $null)
	{
		return $false
	}

	foreach ($key in $tags.Keys)
	{
		if ($key -eq "schedule")
		{
			return $true
		}
	}

	return $false
}

function GetSchedule
{
	param
	(
		[Parameter(Mandatory=$false)]
		[System.Collections.Hashtable]$tags
	)
	
	if ($tags -eq $null)
	{
		return $null
	}

	foreach ($key in $tags.Keys)
	{
		if ($key -eq "schedule")
		{
			return $tags[$key]
		}
	}
}

-------------->
$resource = Get-AzureRmResource -Name $vmName -ResourceType "Microsoft.Compute/virtualmachines"

    foreach ($key in $resource.Tags.Keys)
    {
        if ($key -ne "schedule")
        {
            $tags.Add($key, $resource.Tags[$key])
        }
    }
    $scheduleJson = $_[1]
    $tags +=@{"schedule"=$scheduleJson}

<----------------

I'm happy to create a PR if you make me contributor.

Thanks again for sharing your brilliant script and idea!

Jason

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions