VMware Cloud Community
NeenaJim
Enthusiast
Enthusiast
Jump to solution

VM Tag

$fileName = 'C:\Tags\report2.xlsx'
Get-VM |
select Name,
@{Name = "Tags"; Expression = { (Get-TagAssignment -Entity $_).Tag.Name -join '|' } },
@{N = 'vCenter'; E = { ([uri]$_.ExtensionData.Client.ServiceUrl).Host } } |
Export-Excel -Path $fileName -WorksheetName 'Tags'

Get-VM | Where-Object { (Get-TagAssignment $_) -eq $null } |
select Name,
@{N = 'vCenter'; E = { ([uri]$_.ExtensionData.Client.ServiceUrl).Host } } |
Export-Excel -Path $fileName -WorksheetName 'NoTags'

 

I got the above script from this forum to collect the Tag information of VMs. But I am getting the no information for Tag. can someone please help me where is the disconnect ?

NeenaJim_0-1675445999850.png

 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Works perfectly for me.
Are you sure there are Tags assigned to those VMs?


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

View solution in original post

Reply
0 Kudos
11 Replies
LucD
Leadership
Leadership
Jump to solution

Works perfectly for me.
Are you sure there are Tags assigned to those VMs?


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

Reply
0 Kudos
NeenaJim
Enthusiast
Enthusiast
Jump to solution

My intension is to collect the tag information for all the VMs. Whatever the VMs does not have tag, I need to tag them. 
Answer to your question. Yes Many of them are tagged. 

Reply
0 Kudos
NeenaJim
Enthusiast
Enthusiast
Jump to solution

I think we can ignore this case. I have checked this from a different machines and it is working fine.

Thank you for the help

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Did you try to stop/start your PS session?
That sometimes help


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

Reply
0 Kudos
PSScripter
Enthusiast
Enthusiast
Jump to solution

Is there anyway that script can be modified to output each tag in its own column in Excel (rather then separated by |), and add the Tag Category as a header in the file so you can then filter on tag category in the report?

Thank you

Reply
0 Kudos
PSScripter
Enthusiast
Enthusiast
Jump to solution

I have been using this script which I also found on this site, that I modified to add all of my own tag categories. I was just wondering if this can be made so it just gets all of the tag categories, rather than listing them all like this. I dont have them all listed, just some, but now I think I want to get them all, and I could do that by adding them manually, but a script would be better.

$report = @()

foreach($vm in Get-View -ViewType Virtualmachine){

   $vms = "" | Select-Object VMName,VMState,AppVersion,ServerType,Application,Region,Environment,InfraOwner

   $vms.VMName = $vm.Name
   $vms.VMState = $vm.summary.runtime.powerState
   $vms.AppVersion = (Get-TagAssignment -Entity (Get-VIObjectByVIView -VIView $vm) -Category "AppVersion").Tag.Name
   $vms.ServerType = (Get-TagAssignment -Entity (Get-VIObjectByVIView -VIView $vm) -Category "Server Type").Tag.Name
   $vms.Application = (Get-TagAssignment -Entity (Get-VIObjectByVIView -VIView $vm) -Category "Application").Tag.Name
   $vms.Region = (Get-TagAssignment -Entity (Get-VIObjectByVIView -VIView $vm) -Category "Region").Tag.Name
   $vms.Environment = (Get-TagAssignment -Entity (Get-VIObjectByVIView -VIView $vm) -Category "Environment").Tag.Name
   $vms.InfraOwner = (Get-TagAssignment -Entity (Get-VIObjectByVIView -VIView $vm) -Category "Infrastructure Owner").Tag.Name
   $report += $vms

}

$report | Export-Excel C:\Temp\VMTags.xlsx

 

Thanks!

 

Reply
0 Kudos
PSScripter
Enthusiast
Enthusiast
Jump to solution

Great, Ill check those out, thanks LucD

Reply
0 Kudos
PSScripter
Enthusiast
Enthusiast
Jump to solution

Hi All,

Using this version:

https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/How-to-export-tags-category-all-other-...

 

It works really well. One question I have,  I have it connecting to 6 different virtual centers. It gives me output on everything, except 1 of the VCs I get this error, I guess on every VM, so it ends up taking the script ~12 hours to run or something. If I take that VC out, it completes MUCH faster. but that 1 VC gets no tag output either, all blanks

 

Here is the error:

Get-TagAssignment : 2/6/2023 7:26:44 AM Get-TagAssignment               com.vmware.vapi.std.errors.unauthenticated {'messages':
[com.vmware.vapi.std.localizable_message {'id': com.vmware.vapi.endpoint.method.authentication.required,
'default_message': Authentication required., 'args': [], 'params': , 'localized':}], 'data': , 'error_type':
UNAUTHENTICATED, 'challenge': Basic realm="VAPI endpoint",SIGN
realm=ad091aa8c019850e8e945f742dcc46afeb8211a6,service="VAPI
endpoint",sts="https://virtualcenter"}.
At C:\temp\ITScript\vSphere\vms\Get-VMTags2.ps1:13 char:22
+ $assigned = $allVM | Get-TagAssignment
+                      ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-TagAssignment], CisException
    + FullyQualifiedErrorId : VMware.VimAutomation.ViCore.Impl.V1.Service.Tagging.Cis.TaggingServiceCisImpl.GetTagAssi
   gnment.Error,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.GetTagAssignment

 

Any ideas why that 1 virtual center would give these errors?

 

Thank you.

 

Reply
0 Kudos
PSScripter
Enthusiast
Enthusiast
Jump to solution

PS, I DID tweak the script slightly to add virtualcenter colomun:

$vc = (Get-Content C:\temp\ITScript\vSphere\vcs.txt)
#
# Get a list of all VMs in connected vCenter
#
foreach ($v in $vc){
Connect-VIServer $v
}
$clusters = (Get-Content C:\temp\ITScript\vSphere\clusters.txt)
$allVM = Get-Cluster $clusters | Get-VM | Sort-Object -Property Name
#
# Get all Categories & Tags
#
$assigned = $allVM | Get-TagAssignment
$ordered = $assigned.Tag.Category.Name | Sort-Object -Unique

#
# Cycle through all VMs and associate the Categories & Tags to those with
# Categories & Tags assigned.
#
$allVM | ForEach-Object -Process {
  $obj = [ordered]@{
    VM = $_.Name
    PowerState = $_.PowerState
    Notes = $_.Notes
    Guest = $_.Guest.OSFullName
    NumCpu = $_.NumCpu
    CoresPerSocket = $_.CoresPerSocket
    MemoryMB = $_.MemoryMB
    MemoryGB = $_.MemoryGB
    VMHostId = $_.VMHostId
    VMHost = $_.VMHost.Name
    Folder = $_.Folder.Name
    Version = $_.Version
    GuestId = $_.GuestId
    UsedSpaceGB = $_.UsedSpaceGB
    ProvisionedSpaceGB = $_.ProvisionedSpaceGB
    VirtualCenter = $_.Uid.Split(":")[0].Split("@")[1]
  }

  # Create a column header using the Categories.
  $ordered |
  ForEach-Object -Process {
    $obj.Add($_, '')
  }
  $assigned | Where-Object { $_.Entity.Name -eq $obj.VM } |
  Group-Object -Property { $_.Tag.Category.Name } |
  ForEach-Object -Process {
    $obj[$_.Name] = $_.Group.Tag.Name -join '|'
  }
  New-Object -TypeName PSObject -Property $obj
} | Export-Csv -Path "C:\temp\VMTag-export.csv" -NoTypeInformation -UseCulture

 

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not really, but I have encountered that error occasionally as well.

Sometimes a restart (stop/start) of the PS session helped, sometimes I needed to restart services on the VCSA, and in the worst case the complete VCSA.


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