VMware Cloud Community
RMPatrice
Contributor
Contributor

I am trying to get the TAG assignment to work for getting the Business Unit in the below script.

I am using the script below. I am trying to get the BU information from the Vcenter Tag.  The BU information using custom field works however many of the VMs don't have the BU there. All VMs have the BU in the Tag which is what I am trying to implement in this script. Everything else reports fine and is commented out while testing the TAG 

$report = @()
$vmlist = @()
$vmlist = @(Get-VM)
foreach ($vm in $vmlist) {
$vms = Get-VM $vm.Name | Select-Object Name,
#@{N = "Operating System"; E = { $VM.Guest.OSFullName} },
#@{N = "CPU Count"; E = { $vm.NumCpu } },
#@{N = "Cores Count"; E = { $vm.CoresPerSocket } },
#@{N = "Memory"; E = { $vm.MemoryGB } },
#@{N = "IP 1"; E = { $vm.Guest.IPAddress[0] } },
#@{N = "IP 2"; E = { $vm.Guest.IPAddress[1] } },
#@{N = "VMwareTools Version"; E = { $VM.Guest.ToolsVersion} },
#@{N = "Hardware Version"; E = { $VM.HardwareVersion} },
#@{N = "Cluster";E = {Get-Cluster -VMHost $vm.VMhost | select -ExpandProperty Name } } ,
#@{N = "Host"; E = { ($vm.VMHost) } },
#@{N = ”Datastore”;E={$_.ExtensionData.Config.DatastoreUrl.Name}},
#@{N = ”Datastore Capacity(GB)”;E={Get-Datastore -VM $_ | Select -ExpandProperty CapacityGB}},
#@{N = ”Datastore Free Space(GB)”;E={Get-Datastore -VM $_ | Select -ExpandProperty FreeSpaceGB}},
#@{N = "ProvisionedSpaceGB"; E = { [math]::Round($_.ProvisionedSpaceGB, 1) } },
#@{N = "Folder"; E = { $vm.Folder.Name } },
@{N = "BU"; E = {$vm.TagAssignment.Category("BU")} },
@{N = "Notes"; E = { $vm.notes } }
$report += $vms
}

$report |Export-Csv C:\temp\testing\vmaudit.csv -NoTypeInformation -UseCulture

 

0 Kudos
4 Replies
LucD
Leadership
Leadership

You don't seem to be doing a Get-TagAssignment in this script.
And afaik the VirtualMachine .NET object doesn't have a TagAssignment property.


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

0 Kudos
RMPatrice
Contributor
Contributor

Thanks for your input....I am very new to this and stumbling my way through trial and error....

I was using the custom field @{N = "BU"; E = {$vm.CustomFields.Item("Business Unit")} } which worked fine however on many VMs the field is missing so nothing is pulled.

 

I know that the Tags have been entered for all and was trying to substitute  that into the script but obviously it won't work like that

0 Kudos
LucD
Leadership
Leadership

I see.
You could try with

@{N = "BU"; E = {(Get-TagAssignment -Entity $vm -Category 'BU').Tag.Name}},


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

0 Kudos
RMPatrice
Contributor
Contributor

Luc

This is perfect. 

 

Thanks so much. I owe you a cold beer

0 Kudos