VMware Cloud Community
DZ1
Hot Shot
Hot Shot
Jump to solution

Assigning tags from a csv file

I am able to obtain the custom attributes from the Windows vSphere client, and create tags from them.  What I want to do now is create and assign tags with a csv file, or at least assign the tags from the file, even if I have to create them separately.  I looked at this post Using vSphere Tags with PowerCLI | VMware PowerCLI Blog - VMware Blogs, but I didn't see exactly what I wanted.  I think I may be overcomplicating things.  Here is what I have been testing.

I have a csv file, with two values "VM" and "Info".  The VM column is of course the name of the VM, the Info column is the name of the tag, so I have a variable called $csv

I can try Get-VM $csv.vm and the 3 VMs I have in the variable come up.  I can also get tags with Get-Tag $csv.info, and the 3 tags come up.  What I want to do is use the file to get the VMs, and then assign the tag to those VMs with the same $csv variable. 

I have tried many different things, here are a few:

($csv.vm) | foreach -pv vm -Process { (Get-VM -Name $_.ToString()) } |

foreach -Process { New-TagAssignment -Entity $vm -Tag ($csv.info |

foreach { $_.ToString() } ) -WhatIf }

$csv.info | foreach -pv t { Get-Tag -Name $t } | foreach { New-TagAssignment -Tag $t -Entity ( Get-VM -Name $csv.vm[0,1] ) -WhatIf }

Get-vm $csv.vm | foreach { New-TagAssignment -Entity $_ -Tag ($csv.info | foreach { Get-Tag -Name $_ }) }

I had some more failed attempts, but I crashed the ISE and for some reason it didn't launch with the unsaved tabs (PowerGUI), it usually does, because I've crashed it before and it always came back with the script tabs.  Oh well.

I'll keep going at it, but I hope someone can help.  I just feel that I'm very close. 

0 Kudos
1 Solution

Accepted Solutions
DZ1
Hot Shot
Hot Shot
Jump to solution

I think I have it...I'm going to run it again, but the data all matches up.

$csv | foreach {

  $vm = $_.vm

  $tag = $_.info

  New-Tag -Category Info -Name $tag

  New-TagAssignment -Tag $tag -Entity $vm

}

Thanks to everyone for the help over the years.

View solution in original post

0 Kudos
4 Replies
DZ1
Hot Shot
Hot Shot
Jump to solution

I think I have it...I'm going to run it again, but the data all matches up.

$csv | foreach {

  $vm = $_.vm

  $tag = $_.info

  New-Tag -Category Info -Name $tag

  New-TagAssignment -Tag $tag -Entity $vm

}

Thanks to everyone for the help over the years.

0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

I'm hoping that this will help someone, I have had many questions answered, so I'm hoping that I can help someone with what I did.

I needed to get VMs and their custom attributes from a specific field, so I wrote this.

#“TestData” is the custom attribute

Get-VM  | where { ($_.customfields | where { $_.key -eq "TestData" } | select -ExpandProperty Value).length -gt 0 } | foreach {

( $_.name, ($_.customfields | where { $_.key -eq "Info" } | select -ExpandProperty Value) ) -join ","

} | out-file c:\test\InfoField.txt

What that did was only get VMs where the custom attribute "TestData" had some value.  Then it is saved to a text file..I guess I could have just exported to a csv at that time, but I was just typing and changing things as I went.

That produced a file with the VMname, a comma for the delimiter, and the custom attribute data that will eventually be the new tag.

VMName,custom attribute data

VMName,Custom attribute data

Then I created two columns at the top of the test file, so now the file is:

VM,Info

VMName,custom attribute data

VMName,Custom attribute data

Then I just changed the file extension from .txt to .csv.

So the VMName is of course the name of the VM, and the custom attribute is going to be the name of the tag.

I imported the csv and assigned it to a variable

$csv = Import-CSV C:\Test\InfoField.csv

Now, I just ran this

$csv | foreach {

  $vm = $_.vm

  $tag = $_.info

  New-Tag -Category Info -Name $tag

  New-TagAssignment -Tag $tag -Entity $vm

}

It creates a new tag for each piece of data that was your custom attribute data, and it assigns it to the VM that had that data.

The only issue I had was some of that data had crazy "/" and other characters, and looking back, I think only 1 or 2 VMs had an error, and they can just be input manually.  But overall, it worked.  Thanks everyone...I'm glad I figured it out before anyone answered. This community has helped me out a lot.

0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

I had a typo, the field "TestData" should be "Info"  I was changing some data around for privacy, but I put the change in the wrong area.  So the custom attribute field is called info

0 Kudos
DZ1
Hot Shot
Hot Shot
Jump to solution

I cleaned up everything, and here is the entire script.  It feels odd replying to myself, I feel like I'm entering a Star Trek log.  Ensign PowerCLI newbie, Stardate:  -308572.8173833079

#My custom attribute field is called test, it's the "$_.key" data.  Rename yours as necessary

Get-VM | where { ($_.customfields | where { $_.key -eq "Test" } | select -ExpandProperty Value).length -gt 0 } | Select @{ N="VM"; E={ $_.name } },

@{ N="Tag"; E={ ( $_.customfields | where { $_.key -eq "Test" } | select -ExpandProperty Value) } } | Export-Csv -NoTypeInformation c:\Test\test1.csv

$csv = Import-Csv c:\Test\test1.csv

#Create the tag category first, my category is named "Test"

$csv | foreach {


  $vm = $_.VM

  $tagToAssign = $_.Tag

  New-Tag -Category Test -Name $tagToAssign

  New-TagAssignment -Tag $tagToAssign -Entity $VM

}

I hope someone benefits from this, just trying to help the community that has helped me.

0 Kudos