Im trying to copy all Tags from 1 vCentre to another, while my script works it also throws up a lot of errors when checking if the tag category & tags already exist. Can anyone suggest where I'm going wrong? I assume I'm using | out-null incorrectly?
Function CreateCategories {
$LocalVC = "localvc"
$RemoteVC = "remotevc"
$localCategories = Get-TagCategory -server $LocalVC
ForEach ($Category in $localCategories)
{
If ((get-tagcategory $Category.Name -server $RemoteVC | Out-Null) -eq $null) {New-TagCategory -Name $Category.Name -server $RemoteVC} else { Write-Output "Category $Category.Name Already Exists"}
$localtags = Get-TagCategory $Category.Name -server $LocalVC | Get-Tag
ForEach ($tag in $localtags)
{
If ((Get-TagCategory -Name $Category.Name -server $RemoteVC | get-Tag -Name $tag.Name | Out-Null) -eq $null) {Get-TagCategory -Name $Category.Name -server $RemoteVC | New-Tag -Name $tag.Name} else { Write-Output "Tag $tag.Name already exists"}
}
}
Did you try adding -ErrorAction SilentlyContinue on the cmdlets that give errors?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Did you try adding -ErrorAction SilentlyContinue on the cmdlets that give errors?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Run Disconnect-VIServer -Server * -Force before running and after running on each vCenter. I had a similar issue where PowerCLI didn't understand that it should run in another vCenter. Even $defaultviserver indicating vCenter X, get-tag listed was returning tags on vCenter y
Hi,
not sure but I would try to compare the local categories with the remote categoriere
$localCategories = Get-TagCategory -server $LocalVC
$remoteCategories = Get-TagCategory -server $RemoteVC
ForEach($Category in $localCategories.Name)
{
If ($remoteCategories.Name -notcontains $Category.Name)
{
Write-Output "yeah so I should create it..."
}
else
{
Write-Output "seems to exist nothing to to here..."
}
}
If it helps I would appreciate a kudo 🙂
Thanks, exactly what i needed