PowerCLI

 View Only

 Need a script to copy the attribute value as Tag

Tharun NC's profile image
Tharun NC posted Jul 11, 2024 04:22 AM

Hi Guys,

I am in need of a script that can retrieve the value of the attribute NB_LAST_BACKUP and add it as a tag to the VM. The MID server will then collect the tag and update the service-now record of the VM. I plan to run the script every 24 hours to obtain the last backup date.
 
The tag category name can be LastBackupDate, and tag need to be the current value of the attribute, which will be updated by Veritas Netbackup after a successful backup of the VM.


LucD's profile image
LucD

You mean something like this?

$attrName = 'NB_LAST_BACKUP'
$tagCatName = 'LastBackupDate'

$attr = Get-CustomAttribute -Name $attrName
try{
    $cat = Get-TagCategory -Name $tagCatName -ErrorAction Stop
}
catch{
    $cat = New-TagCategory -Name $tagCatName -EntityType VirtualMachine -Cardinality Single
}

Get-VM -PipelineVariable vm | Get-Annotation -CustomAttribute $attr |
ForEach-Object -Process {
    New-TagAssignment -Entity $vm -Tag (New-Tag -Name $_.Value -Category $cat -Confirm:$false) -Confirm:$false
}
Tharun NC's profile image
Tharun NC

Thanks @LucD for the fantastic script. It works perfectly but has a problem. The value of NB_LAST_BACKUP gets updated on every backup but the script is unable to update the new value on re-run as the cardinality is single and if we set it multiple, then there will be multiple values added to the VM. In that case, can you update the script so the existing tag is removed before tagging the new value? Also is there any command that can be used to delete all the tags without assignment? it will help me delete all the unnecessary tags that are created every day with the date format which will not be used again.

Get-TagAssignment -entity $Vm -Category LastBackupDate |Remove-Tagassignment -Confirm:$false

LucD's profile image
LucD

You can add a Remove-TagAssignment, something like this

$attrName = 'NB_LAST_BACKUP'
$tagCatName = 'LastBackupDate'

$attr = Get-CustomAttribute -Name $attrName
try{
    $cat = Get-TagCategory -Name $tagCatName -ErrorAction Stop
}
catch{
    $cat = New-TagCategory -Name $tagCatName -EntityType VirtualMachine -Cardinality Single
}

Get-VM -PipelineVariable vm | Get-Annotation -CustomAttribute $attr |
ForEach-Object -Process {
    Get-TagAssignment -Entity $vm -Category $cat |
    ForEach-Object -Process {
        Remove-TagAssignment -TagAssignment $_ -Confirm:$false 
    }
    New-TagAssignment -Entity $vm -Tag (New-Tag -Name $_.Value -Category $cat -Confirm:$false) -Confirm:$false
}
Tharun NC's profile image
Tharun NC

I get this message when I run the updated script.

LucD's profile image
LucD

Oops typo, I forgot to add the TagAssignment parameter.
I corrected the code

Tharun NC's profile image
Tharun NC

That worked like a charm. Thanks a lot. Do you have any blog\site that I can follow?

LucD's profile image
LucD

You're welcome.

Yes, I do (lucd.info).
The link is normally in my footer, but it seems the footer is only displayed when the thread is a Discussion, not when it is a Question (like this thread).

On a side note, since my retirement I haven't created any new posts.