VMware Cloud Community
BjornJohansson
Enthusiast
Enthusiast
Jump to solution

Importing vSphere tags from CSV gets incorrectly applied if some values in CSV are empty

Hi all,

  • vCenter 6.0 and 6.5,
  • PowerCli 6.5 r1

I need to import vSphere Tags from a csv into my vCenter and apply this to relevant VM's.

Not all VM's should have tags assigned, some should have all tags. The script is applying the latest assigned Tag to the following VM's who are missing have not a Tag specified in the CSV. Please see below for example.

The script is taken from: http://www.virtu-al.net/2014/11/13/automating-tags-tag-category-creation-assignment-powercli/

Example:

This is the CSV.  The CostCenter "120" will get applied to the VM's under it who are missing the CostCenter Tag info. In this case the bold marked VM's will incorrectly get the CostCenter 120 Tag.

Name,SystemOwner,CostCenter,BackupJob

test-vm6,BB,200,backup-Daily

test-vm5,PB,1,backup-Daily

test-srv2,,220,

test-vm3,,200,Backup-weekly

test-vm4,,210,Backup-testing

av-srv2,,666,

test-vm1,,120,

test-srv1,,,

2012-tpl,,,Backup-manual

vcenter-01,,,

I could just add some dummy info like NoCostCenter, NoBackup, NoOwner to these VM's. But if there is another way it would be great.

Thank you in advance.

B

1 Solution

Accepted Solutions
kwhornlcs
Enthusiast
Enthusiast
Jump to solution

Script isn't accounting for empty tags. Using the script from the original reference, change this line (on or around line 21):

$cmdbinfo | Where {$_.($Name) -eq $Tag} | Foreach {

to:

$cmdbinfo | Where {($tag) -and ($_.($Name) -eq $Tag)} | Foreach {

and see what you get.

View solution in original post

2 Replies
kwhornlcs
Enthusiast
Enthusiast
Jump to solution

Script isn't accounting for empty tags. Using the script from the original reference, change this line (on or around line 21):

$cmdbinfo | Where {$_.($Name) -eq $Tag} | Foreach {

to:

$cmdbinfo | Where {($tag) -and ($_.($Name) -eq $Tag)} | Foreach {

and see what you get.

BjornJohansson
Enthusiast
Enthusiast
Jump to solution

That was it!

Thank you! Smiley Happy

BL

Reply
0 Kudos