Norrin Rad
Norrin Rad

Reputation: 991

Assign Tags using Powershell and reading from CSV

I have a bit of a tricky one.

I have a csv file which has the following headers. I'm trying to assign the vmname in the csv the tags as mentioned in the csv under the relevant header (hope that makes sense).

vmname, resourcegroup, size, costcenter, displayname etc

When I run the first 20 lines it produces the azure text file as expected.

However if I run the second block lines 23 onwards to assign the tags to the vm it adds the key values but not the actual value and shows as blank in tags on the portal through the gui.

I'm not sure what I'm doing wrong and wondered if anyone can see what I'm doing, the only thing I think it could be the $vm = $_.vmname, but can't see how.

The code I have is below

$csv = import-csv "C:\temp\book.csv"
$csv | foreach-object {
  $first =$_.VMName
  $Second =$_.ResourceGroup
  $size=$_.Size
  $t1= $_.Costcenter
  $t2= $_.Displayname
  $t3= $_.Environment
  $t4= $_.Project
  $t5= $_.Role
  $t6= $_.Template
  $t7= $_.DSC
  $t8= $_.Schedule
  $t9= $_.AppID
  $t10= $_.Service
  $t11= $_.REF
  $t12= $_.OS
  $t13= $_.Zone
  "The VM is $first, the RG is $second, the size is $size and tags $t1, $t2, $t3, $t4, $t5, $t6, $t7, $t8, $t9, $t10, $t11, $t12, $t13" | out-file C:\temp\Azure.txt -append
  }
##the above works fine

Select-AzureRmSubscription -SubscriptionId "xxxxxx-xxxx-xxxx-xxxx-xxxxxxx"
$csv = import-csv "C:\temp\book.csv"
$vm = "$_.VMName"
$tags = (Get-AzureRmResource -ResourceGroupName "RG01" -ResourceType "Microsoft.Compute/virtualMachines"   -Name "$VM").Tags
$csv | ForEach-Object {
  $first =$_.VMName
  $t1= $_.Costcenter
  $t2= $_.Displayname
  $t3= $_.Environment
  $t4= $_.Project
  $t5= $_.Role
  $t6= $_.Template
  $t7= $_.DSC
  $t8= $_.Schedule
  $t9= $_.AppID
  $t10= $_.Service
  $t11= $_.REF
  $t12= $_.OS
  $t13= $_.Zone

$tags += @{
Costcenter="$t1"
Displayname="$t2"
Environment="$t3"
Project="$t4"
Role="$t5"
Template="$t6"
DSC="$t7"
Schedule="$t8"
AppID="$t9"
Service="$t10"
DREF="$t11"
OS="$t12"
Zone="$t13"
    }
}

Set-AzureRmResource  -ResourceGroupName "RG01" -Name "$VM" -Tag $tags -ResourceType "Microsoft.Compute/virtualMachines" -verbose

Thanks in advance :)

Upvotes: 0

Views: 2062

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174845

It looks like you're over-complicating this task a bit.

If I understand correctly, what you want to do (in pseudo-code) is:

foreach $VM in $CSVfile
    Retrieve existing tags for $VM
    Add csv values to existing tags
    Set new tag set on $VM in azure

This should be pretty straightforward:

Select-AzureRmSubscription -SubscriptionId "xxxxxx-xxxx-xxxx-xxxx-xxxxxxx"
$csv = import-csv "C:\temp\book.csv"

$csv | ForEach-Object {
    # Retrieve existing tags
    $tags = (Get-AzureRmResource -ResourceGroupName "RG01" -ResourceType "Microsoft.Compute/virtualMachines" -Name $_.VMName).Tags
    # Add new value pairs from CSV
    $tags += @{
        Costcenter  = $_.Costcenter
        Displayname = $_.Displayname
        Environment = $_.Environment
        Project     = $_.Project
        Role        = $_.Project
        Template    = $_.Template
        DSC         = $_.DSC
        Schedule    = $_.Schedule
        AppID       = $_.AppID
        Service     = $_.Service
        DREF        = $_.REF
        OS          = $_.OS
        Zone        = $_.Zone
    }

    # Update resource with new tag set
    Set-AzureRmResource  -ResourceGroupName "RG01" -Name $_.VMName -Tag $tags -ResourceType "Microsoft.Compute/virtualMachines" -verbose
}

If any of the tag names from your CSV file already exist as tags on the resource, make sure you add them manually, one at a time:

Select-AzureRmSubscription -SubscriptionId "xxxxxx-xxxx-xxxx-xxxx-xxxxxxx"
$csv = import-csv "C:\temp\book.csv"

$csv | ForEach-Object {
    # Retrieve existing tags
    $tags = (Get-AzureRmResource -ResourceGroupName "RG01" -ResourceType "Microsoft.Compute/virtualMachines" -Name $_.VMName).Tags

    # Define new value pairs from CSV
    $newTags = @{
        Costcenter  = $_.Costcenter
        Displayname = $_.Displayname
        Environment = $_.Environment
        Project     = $_.Project
        Role        = $_.Project
        Template    = $_.Template
        DSC         = $_.DSC
        Schedule    = $_.Schedule
        AppID       = $_.AppID
        Service     = $_.Service
        DREF        = $_.REF
        OS          = $_.OS
        Zone        = $_.Zone
    }

    # Add new tags to existing set (overwrite conflicting tag names)
    foreach($tagName in $newTags.Keys){
        $tags[$_] = $newTags[$_]
    }

    # Update resource with new tag set
    Set-AzureRmResource  -ResourceGroupName "RG01" -Name $_.VMName -Tag $tags -ResourceType "Microsoft.Compute/virtualMachines" -verbose
}

Upvotes: 1

Related Questions