Hi TravisRoberts,
As you have discovered, this is the default behavior when re-registering a VM (tags will be lost). You will need to backup the tags before removing the VMs from inventory.
Example - How to save your tags
Get-VM myVM | Select Name,@{N="Tags";E={((Get-TagAssignment -Entity $_ | select -ExpandProperty Tag).Name -join ",")}} | Export-Csv -NoTypeInformation c:\temp\myTags.csv
Example - Assign Tags from the CSV you saved previously:
## AssignVMTags.ps1
Write-Host "`nThis script assigns vSphere tags to one or more VMs" -ForegroundColor Yellow
$inputfile = Read-Host "Enter path to your Tag Assignment CSV"
foreach ($row in (Import-Csv $inputfile) | Where-Object { $_ -ne "Name" }) {
$vmName = $row.Name
$vmTags = $row.Tags
If (Get-View -ViewType virtualmachine -Filter @{ "Name" = "$($vmName)"; "Config.Template" = "True" }) {
$vm = Get-Template -Name $vmName
}
Else {
$vm = Get-VM -Name $vmName
}
foreach ($vmTagName in ($vmTags.Split(","))) {
Write-Host "..Assigning the $($vmTagName) tag to $($vm.name)" -ForegroundColor Green
$vmTag = Get-Tag -Name $vmTagName
New-TagAssignment -Tag $vmTag -Entity $vm | Out-Null
}
}