$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 ?
Works perfectly for me.
Are you sure there are Tags assigned to those VMs?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Works perfectly for me.
Are you sure there are Tags assigned to those VMs?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
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
Did you try to stop/start your PS session?
That sometimes help
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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
Yes, there are a couple of similar requests in
Solved: Re: How to export tags/category + all other defaul... - VMware Technology Network VMTN
Solved: Re: Exporting VM Tags in readable format - VMware Technology Network VMTN
Solved: Re: Export Tags and Notes in to a single CSV - VMware Technology Network VMTN
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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!
Great, Ill check those out, thanks LucD
Hi All,
Using this version:
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.
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
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