VMware Cloud Community
pamiller21
Enthusiast
Enthusiast

Tag Migrations

I am trying to migrate VMs from one vcenter to another and I have a CSV of the source VMs and want to make sure I know how to import them to the destination vCenter.

$tagNames = Get-TagCategory | %{$_.Name}

Get-VM | where{$_.Notes -notmatch 'This is a test VM'} |ForEach-Object -Process {

    $obj = [ordered]@{

        Name = $_.Name

        Notes = $_.Notes

    }

    $tagNames | %{

        $obj.Add($_,'')

    }

    Get-TagAssignment -Entity $_ | %{

        $obj.Item($_.Tag.Category.Name) = $_.Tag.Name

    }

    New-Object PSObject -Property $obj

} | Export-Csv "\Tags-Assignemnts.csv" -NoTypeInformation -UseCulture

0 Kudos
3 Replies
LucD
Leadership
Leadership

Is that export working for you?

You only seem to allow 1 tag per category.
Have a look at this alternative scrip Re: Exporting VM Tags in readable format

Do you also need the import script?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
pamiller21
Enthusiast
Enthusiast

That worked great, but ya could I get a import then?

0 Kudos
LucD
Leadership
Leadership

You could try something like this.

It assumes that you exportfile looks like this.

Name,Notes,Cat1,Cat2,Cat3

vm1,'Notes',Tag1,,Tag3

vm2,'Notes2',Tag1,Tag2,Tag3

vm2,'Notes3',,,Tag3

$rows = Import-Csv -Path .\tag-export.csv -UseCulture

$catNames = $rows | Get-Member -MemberType NoteProperty |

   where {'Name', 'Notes' -notcontains $_.Name} |

  Select -ExpandProperty Name

foreach ($catName in $catNames) {

   Try {

   $cat = Get-TagCategory -Name $catName -ErrorAction Stop

   }

   Catch {

   $cat = New-TagCategory -Name $catName

   }

}

foreach ($row in $rows) {

   $vm = Get-VM -Name $row.Name

   foreach ($catName in $catNames) {

   if ($row."$catName" -ne '') {

   $cat = Get-TagCategory -Name $catName

   Try {

   $tag = Get-Tag -Name $row."$catName" -Category $cat -ErrorAction Stop

   }

   Catch {

   $tag = New-Tag -Name $row."$catName" -Category $cat

   }

   New-TagAssignment -Entity $vm -Tag $tag

   }

   }

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos