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
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
That worked great, but ya could I get a import then?
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