VMware Cloud Community
Mike_Yazbeck
Enthusiast
Enthusiast
Jump to solution

Get-VM using multiple tags to get a specific tag after that can be filtered

Hi Guys,

I am currently having an issue where the actual results of a query isnt returning all the VMs I want, instead I get too many and then have to filter out the ones I want later in Excel which I want to avoid.

This is the query:

Get-VM -Tag preproduction,itdevelopment,development,bau, | Where-Object {$_.PowerState -eq "PoweredOn"} | Select Name,@{N="Tags";E={((Get-TagAssignment -Entity $_ | select -ExpandProperty Tag) | Where-Object {$_ -match "RPO/Other"})}} | ft -AutoSize

I want to get the name of the array, not the contents of the array itself, the source I used comes from here: PowerCLI One-Liner to Get VM Tags | vmkdaily

However, every time I look at my output, it comes out like this:

Name  Tags
----  ----
server1    
server2    
server3    

server4    RPO/Other

server5    
server6    
server7    

server8    RPO/Other

server9    RPO/Other

server10  RPO/Other

server11  RPO/Other

I just want it to equal "RPO/Other", but when I do that, no results come up at all 😕

Please help.

34 Replies
LucD
Leadership
Leadership
Jump to solution

That is controlled by the format (.ps1xml) files that come with PowerCLI.

For a TagAsignment object, this is the definition

So two columns, Tag & Entity

And in those columns the output engine will show the Tag property (which cna be an array), and the entity name

Note that you can do some very confusing things with such formatting files

tagassign.png


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

Reply
0 Kudos
Mike_Yazbeck
Enthusiast
Enthusiast
Jump to solution

Thats interesting, I didnt know that was even under the hood Smiley Happy

It still doesnt explain the bizarre issue im getting though...

Technically it does show the hostname, but it produces all those crazy columns in addition to that array.

The one thing I have just noticed is that some (not all) of those columns are actually Tag Categories!!! :smileyconfused:

Do you think this might be a bug?

EDIT: One thing I should add, is that all of our VMs and Virtual Appliances each have a minimum of 5 tags approximately.

All I want to do is remove a tag and assign a new one based on its name, there doesnt appear to be an easy way to do this (using thorough error checking etc...)

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That might indeed be a bug.


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

Reply
0 Kudos
Mike_Yazbeck
Enthusiast
Enthusiast
Jump to solution

Looks like its definitely been confirmed as a bug:

Get-TagAssignment Entity includes custom field names (Bug?)

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Nice find, didn't remember that one :smileyblush:


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

Mike_Yazbeck
Enthusiast
Enthusiast
Jump to solution

Lets hope VMWare come up with a fix soon Smiley Happy

Reply
0 Kudos
Mike_Yazbeck
Enthusiast
Enthusiast
Jump to solution

Just to keep you all posted, I logged a call with VMWare about this as it started to get annoying, plus I have seen screenshots via google where they expect to see the Entity displayed as a hostname.

I will let you know what VMWare say Smiley Happy

Reply
0 Kudos
sushilkm
Enthusiast
Enthusiast
Jump to solution

LucD

So I adapted the above code for myself and was able to find the VM;s with particular tag. Now problem is that we have tags set to true and false. Is there any way that i can customize this code to extract only those VM's which have a particular tag set to "TRUE"

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not sure I'm following here.
The Tag system works with a TagCategory and a Tag name, no assigned values afaik.
Or are you referring to the Description field of the Tag?
Or are you perhaps referring to Custom Attributes?

Perhaps a screenshot of such a Tag that you want to list would help.


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

Reply
0 Kudos
sushilkm
Enthusiast
Enthusiast
Jump to solution

HI LucD

Apologies for late revert. We are trying to export below in red box.

Tags.png

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No problem.

Looks like you are using the Tag as the value for the TagCategory.

So you could do

Get-Tag -Name FALSE -Category monitoring


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

Reply
0 Kudos
sushilkm
Enthusiast
Enthusiast
Jump to solution

Cool.. Let me try and feedback...

Reply
0 Kudos
sushilkm
Enthusiast
Enthusiast
Jump to solution

Unable to make it work for some reason... Smiley Sad

PS C:\> get-VM * | Get-Tag -Name FALSE -Category monitoring

Get-Tag : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not

match any of the parameters that take pipeline input.

At line:1 char:12

+ get-VM * | Get-Tag -Name FALSE -Category monitoring

+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (oraracq19-01:PSObject) [Get-Tag], ParameterBindingException

    + FullyQualifiedErrorId : InputObjectNotBound,VMware.VimAutomation.ViCore.Cmdlets.Commands.Tagging.GetTag

Below is my PS version

Name                           Value

----                           -----

PSVersion                      5.1.14409.1012

PSEdition                      Desktop

PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}

BuildVersion                   10.0.14409.1012

CLRVersion                     4.0.30319.42000

WSManStackVersion              3.0

PSRemotingProtocolVersion      2.3

SerializationVersion           1.1.0.1

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That code was not intended to have Get-VM piped to it.


If you want to select the VM that have the tag FALSE in the category monitoring, you should do

Get-VM |

where{Get-TagAssignment -Entity $_ -Category monitoring | where{$_.Tag.Name -eq 'FALSE'}}


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

sushilkm
Enthusiast
Enthusiast
Jump to solution

Awesome. Exactly the medicine, doctor would be proud of post a life threatening disease :smileysilly:

Reply
0 Kudos