VMware Cloud Community
ncouu
Contributor
Contributor

VMs without a tag will inherit the tag of the parent folders.

Hello,

I'm trying to get a CSV file with all the tags of the VMS on a vCenter, I can do that.

I can get another CSV with the VMS that doesn't have a tag.

What I need is a CSV file that will show all the VMS with their tags, and the VMS that doesn't have one... should inherit it from their parent folder.

I hope I explained myself properly and someone can lend me some help on this one.

Thanks!

0 Kudos
5 Replies
LucD
Leadership
Leadership

Just to clarify, you are talking about VM-type folders when you mention "parent foder"?


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

0 Kudos
Dimon_SPB
Contributor
Contributor

like this?

# receiving date for a file name

$CurrentDate = get-date -uformat "%d-%m-%Y"

# obtaining list of categories

$TAGcategory = Get-TAGCategory

## list of categories

$TAGcategory.name

## place of storage of the log

$LOGFile = "C:\Backups\IaaS-TAG_sets_" + $CurrentDate + ".csv"

## initialization of a variable for storage of log

$LOG = New-Object PSObject

Add-Member -InputObject $LOG -MemberType NoteProperty -Name Object -Value "Object name"

Add-Member -InputObject $LOG -MemberType NoteProperty -Name ObjectType -Value "Object type"

## we set quantity of columns = to amount of categories

foreach ($TAGcategory1 in $TAGcategory)

        {

        Add-Member -InputObject $LOG -MemberType NoteProperty -Name $TAGcategory1.Name -Value $TAGcategory1.Name

        }

## VM processing

$FL1 = Get-Folder -name "rootname1" -Type VM | get-vm | ?{$_.PowerState -eq "PoweredOn"}

$FL2 = Get-Folder -name "rootname2" -Type VM | get-vm | ?{$_.PowerState -eq "PoweredOn"}

$FL3 = Get-Folder -name "someparentname3" -Type VM | get-vm | ?{$_.PowerState -eq "PoweredOn"}

$FL4 = Get-Folder -name "somefoldername4" -Type VM | get-vm | ?{$_.PowerState -eq "PoweredOn"}

$allVM = ($FL1 + $FL2 + $FL3 + $FL4) | Sort-Object

foreach ($VM in $allVMs)

    ## record in the log file

    $LOG.Object = $VM.name

    $LOG.ObjectType = "virtual machine"

foreach ($TAGcategory1 in $TAGcategory)

        {

        $LOG.$TAGcategory1 = ($VM | Get-TagAssignment –Category $TAGcategory1).Tag.name

        }

    ## record in the log file of a resultant line

    Export-Csv -path $LOGFile  -InputObject $LOG -Encoding UTF8 -Append

}

Disconnect-VIServer -Confirm:$false

exit

0 Kudos
ncouu
Contributor
Contributor

Yes, VM Type folder.

0 Kudos
ncouu
Contributor
Contributor

Thanks for replying,

The script that you shared only returns ToolVersion and HWVersion.

I'm looking for something that will return, VMName, VMType Folder that belongs to, and Tag.

Regards

0 Kudos
LucD
Leadership
Leadership

Try something like this

Get-VM |

Select Name,

   @{N='Folder';E={$_.Folder.Name}},

   @{N='Tag';E={

   (Get-TagAssignment -Entity $_).Tag.Name,

   (Get-TagAssignment -Entity $_.Folder).Tag.Name |

  Select -First 1

   }}


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

0 Kudos