Hi,
I'm using a script to automatically tag new VMs so they get backed up if the user which provisioned the VM forgot about it.
It works for a few months and then, I lose all my VM tags. It's already the second or third time that it occured, which is why I'm opening this thread (I don't really see why this is happening, as my script is only assigning a tag to untagged VMs).
I added the functions I'm using below because I don't understand why it works for so long then for no apparent reasons erase all my VM tags. The only event would be an ESX host that has been added in maintenance mode to the cluster. (but it should not bother the tags as they are managed by the vCenter, right ?)
Here is what my main function looks like (all the parameters are either inputs from the user, or from a configuration file if he forgets about it) :
Function Search-JenkinsUntaggedVMs
{
Param(
[string]$vCenter,
[Parameter(Mandatory = $true)]
$Destinataires,
[string]$TagName
)
#Connecting to the vCenter through a custom function (getting infos from a configuration file)
Connect-vCenter -vCenter $vCenter
#Getting the untagged VMs list through a custom function too)
$VMsUntagged = Get-UntaggedVMs
# it only does : $VMs = Get-VM | ?{ (Get-TagAssignment $_ -Category 'Backup') -eq $null} and returns $VMs
if($VMsUntagged)
{
#Getting the default tag from the configuration file
$configuration = Get-XMLConfig $($global:ScriptDir + "\Configuration.xml")
if([string]::IsNullOrEmpty($TagName))
{
$TagName = $configuration.VMware.DefaultTag
}
foreach($VM in $VMsUntagged)
{
#Then we tag each VM with the next function
Set-VMTag -VMName $VM.Name -TagName $JobName
}
}
else
{
$Message = 'blabla'
}
#Returning the newly tagged VMs list through email...
return Send-Email -Destinataires $Destinataires -Resultats $Message
}
And here is the function doing the VM tagging :
It uses 'try/catch' blocks because it's easier to check if it failed than to check if it already exists. (more 'pythonic' I guess)
Function Set-VMTag
{
Param(
[Parameter(Mandatory = $true)]
[string]$VMName,
[Parameter(Mandatory = $true)]
[string]$TagName
)
try
{
#Check that the category exists, if not I create it
$TagCategory = Get-TagCategory -Name "Backup"
if([string]::IsNullOrEmpty($TagCategory))
{
throw
}
}
catch
{
$TagCategory = New-TagCategory -Name "Backup"
}
try
{
#Check that the tag exists in this category, else I create it too
$Tag = Get-Tag -Name $TagName
if([string]::IsNullOrEmpty($Tag))
{
throw
}
}
catch
{
$Tag = New-Tag -Name $TagName -Category $TagCategory
}
try
{
#Then I tag my untagged VM
New-TagAssignment -Tag $Tag -Entity $(Get-VM -Name $VMName) | Out-Null
return $true
}
catch
{
return $false
}
}
If you see anything wrong with the way I'm doing it, or encountered this kind of problem before please share it .
Thanks
Ok, with only one tag category, leaving out the Category has no effect.
Btw, are you sure nobody did a reset of the Inventory Service, that will also remove all tags?
Do you loose all tags, or only some?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I would need to know which tag categories and tags in each of the categories you are using to do a more detailed analysis.
I did notice that youdo
$Tag = Get-Tag -Name $TagName
which doesn't mention the Category. If that tag exists in any category, you will get an object returned.
That might confuse the logic.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I create a "Backup" category, then every tag I use are in this category.
I create tags like : 'BRONZE', 'SILVER', 'GOLD'... (typical tiering levels) in this category. The default tag is 'BRONZE'
You are right about the Get-Tag command, I should probably use the category too as filter. I know for sure that no other tag uses the same name, even in other categories. (they are only created for the backup at the moment, and through this PowerCLI module).
Ok, with only one tag category, leaving out the Category has no effect.
Btw, are you sure nobody did a reset of the Inventory Service, that will also remove all tags?
Do you loose all tags, or only some?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I lose all tags, which is really strange considering the script works fine for a few months.
I don't think anyone in their right mind would reset the inventory service. I would have to check the actions of the last few days to be 100% sure that no one did something like that.
Having hosts in maintenance mode should have no impact on the tags, right ?
No, ESXi nodes in maintenance mode have no effect on tags.
The tag info is kept in the Inventory Service DB.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for the great help, I'll look at something else than the script then. (PowerCLI is still doing wonders)
What actions could do an Inventory Service reset ? Something like a vCenter upgrade ?
No, an upgrade should keep the Inventory Service DB intact, unless that option was selected.
Do you have a vCenter or a VCSA?
Which version>
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference