VMware Cloud Community
nickbu
Contributor
Contributor
Jump to solution

Script to tag VMs error

Hi Community,

I was hoping you'd be able to help with my script that I am modifying that is currently experiencing errors.

I am trying to modify this script to tag VMs instead of clusters. with the below code & CSV:

VMName,Tag,TagCategory,Cardinality,EntityType

$csv = Import-Csv C:\scripts\VMTagstest.csv

$uniqueTagCategory = $csv | Sort-Object -Property @{Expression='TagCategory'; Descending=$true}, Cardinality, EntityType -Unique
foreach ($data in $uniqueTagCategory)
{
    try
    {
        $newTagCategory = New-TagCategory -Name $data.TagCategory -Cardinality $data.Cardinality -EntityType $data.EntityType -ErrorAction Stop
        Write-Host "New TagCategory created - '$($newTagCategory.Name)'" -BackgroundColor DarkGreen
    }
    catch
    {
        $tagCategory = Get-TagCategory -Name $data.TagCategory -ErrorAction Stop
        $entityType = $tagCategory.EntityType -join ','
        Write-Warning " Already exist - TagCategory '$($tagCategory.Name)' with Cardinality '$($tagCategory.Cardinality)' and Entitytype '$entityType'"
    }
}

foreach ($data in $csv)
{

    $VMName = Get-VM $data.VMName
    $currentConf = Get-TagAssignment -Entity $VMName | Where-Object {$_.Tag.Category.Name -eq $data.TagCategory}
    if ($null -eq $currentConf.Tag.Name)
    {
        $tag = New-Tag -Name $data.Tag -Category $data.TagCategory
        $tagAssignment = $VMName | New-TagAssignment -Tag $tag
        $message = "VM '{0}' assigned New tag '{1}' under TagCategory '{2}'" -f $tagAssignment.Entity.Name, $tagAssignment.Tag.Name, $tagAssignment.Tag.Category.Name
        Write-Host $message -ForegroundColor Yellow
    }
    elseif ($currentConf.Tag.Name -ne $data.Tag)
    {
        $replaceTag = $currentConf.Tag | Set-Tag -Name $data.Tag
        $message = "VM '{0}' current tag '{1}' replaced with '{2}' under TagCategory '{3}'" -f $replaceTag.Entity.Name, $currentConf.tag.Name, $replaceTag.Name, $replaceTag.Category.Name
        Write-Host $message -ForegroundColor Red
    }
    else
    {
        $message = "VM '{0}' is already configured with Tag '{1}' under TagCategory '{2}'" -f $currentConf.Entity.Name, $currentConf.tag.Name, $currentConf.tag.Category.Name
        Write-Host $message -ForegroundColor Green
    }
}

Get-VM | Get-TagAssignment | Select-Object Entity, Tag | Export-Csv c:\scripts\output.csv
 
 
Attached is the output of the script i am getting.
 
 
Hope you can help!
Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That code doesn't take into account that a VM can have multiple tags assigned.
The New-Tag cmdlet should also be placed in a Try-Catch construct.

But why don't you ask the author of that script?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

That code doesn't take into account that a VM can have multiple tags assigned.
The New-Tag cmdlet should also be placed in a Try-Catch construct.

But why don't you ask the author of that script?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

nickbu
Contributor
Contributor
Jump to solution

Thanks for the suggestion LucD. I put it in a try-catch and it worked.

Reply
0 Kudos