Mike_Yazbeck
Enthusiast
Enthusiast

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

Jump to solution

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.

1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Sure, try something like this

Get-VM -Tag preproduction,itdevelopment,development,bau |

Where{$_.PowerState -eq 'PoweredON' -and (Get-TagAssignment -Entity $_ ).Tag -match 'RPO/Other'}


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

View solution in original post

0 Kudos
34 Replies
LucD
Leadership
Leadership

A Select-Object cmdlet produces a complex object, not a single value.

So placing your Where-clause after the Select-Object is not a good idea.

Why don't you combine both conditions in the first Where-clause with and -and operator?


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

0 Kudos
Mike_Yazbeck
Enthusiast
Enthusiast

How do I do it?

When I tried to earlier, I did it as an expression.

Would you mind showing me please Smiley Happy

0 Kudos
LucD
Leadership
Leadership

Sure, try something like this

Get-VM -Tag preproduction,itdevelopment,development,bau |

Where{$_.PowerState -eq 'PoweredON' -and (Get-TagAssignment -Entity $_ ).Tag -match 'RPO/Other'}


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

0 Kudos
Mike_Yazbeck
Enthusiast
Enthusiast

Many thanks Smiley Happy

Im currently upgrading NetBackup to 7.7.3 - give me an hour or so and I will check and let you know Smiley Happy

Worse case scenario I will check tomorrow morning as I leave the office shortly.

0 Kudos
Mike_Yazbeck
Enthusiast
Enthusiast

That seems to work a treat :smileygrin:

Is there any way to get the Tag column listing 'RPO/Other' beside it?

Is there a property I can interrogate that will populate this for me.

I eventually did this but would like to have 2 columns that includes the secondary tag:

Get-VM -Tag preproduction,itdevelopment,development,bau | Where{$_.PowerState -eq 'PoweredOn' -and (Get-TagAssignment -Entity $_ ).Tag -match 'RPO/Other'} | select name

Any ideas?

0 Kudos
LucD
Leadership
Leadership

Not sure I got the full picture.

I assume you have two tags on the VM, you filter on the first one, and want to display the 2nd one. Is that correct?

If yes, try like this

Get-VM -Tag preproduction,itdevelopment,development,bau |

Where{$_.PowerState -eq 'PoweredON' -and (Get-TagAssignment -Entity $_  -OutVariable t).Tag -match 'RPO/Other'} |

Select Name,@{N='Tag';E={$t | where{$_.Tag -notmatch 'RPO/Other'} | select -ExpandProperty Tag}}

If you mean something else, perhaps a screenshot of the tags on a VM you want to  list would clarify?


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

0 Kudos
Mike_Yazbeck
Enthusiast
Enthusiast

Hi LucD,

I was thinking of something more like this:

Get-VM -Tag preproduction,itdevelopment,development,bau | Where{$_.PowerState -eq 'PoweredOn' -and (Get-TagAssignment -Entity $_ -OutVariable t).Tag -match 'RPO/Other'} |

select name, @{N='Tag';E={$t | where{$_.Tag -match 'RPO/Other'} | select -ExpandProperty Tag}} | Format-Table -AutoSize

I do like your one though and will definitely use that in future :smileygrin:

Just out of curiousity, why is it that I can use -match but I cant use -eq even though it is technically letter perfect?

0 Kudos
LucD
Leadership
Leadership

If they are letter perfect, it shouldn't make a difference.

But if there is some whitespace at the beginning or end, the -eq will fail.

Example:

'a ' -match 'a'

'a ' -eq 'a'

I don't immediately see the difference between both solutions I'm afraid.
But that is probably due to the way you have set up your Tags

Update: just noticed you used a -match while I used a -notmatch.

Now I see it


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

Mike_Yazbeck
Enthusiast
Enthusiast

I think I found out why:

$t.GetType()

IsPublic IsSerial Name  BaseType
-------- -------- ----  --------
True True ArrayList System.Object
0 Kudos
LucD
Leadership
Leadership

If you're referring to the -eq and -match question, I don't understand what you mean.

There can be one or more tags in $t, so it can be an array or a single object.

But that doesn't really matter since we pipe the content of $t through the Where-clause.


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

0 Kudos
Mike_Yazbeck
Enthusiast
Enthusiast

That was what I was referring to Smiley Happy

$t contains the "RPO/Other" details, but is the top of the array if that makes sense, its not within the array.

Im now having another problem, I run the following code:

$Tagged_VM = Get-TagAssignment -Entity $Server -Category $Current_Tag_Category

if ($Tagged_VM)

{

    Write-Host "Server: ""$($Tagged_VM.Entity).Name"" is using Tag: ""$($Tagged_VM.Tag.Name)"" is associated with Category: ""$($Tagged_VM.Tag.Category)"""

}

else

{

    Write-Host """$Server"" is not associated with Tag category ""$Current_Tag_Category"""

}

The output comes out as:

Server: "Environment NB_LAST_BACKUP Product SFC Type Server1.Name" is using Tag: "Other" is associated with Category: "RPO"

Server: "Environment NB_LAST_BACKUP Product SFC Type Server2.Name" is using Tag: "Other" is associated with Category: "RPO"

I then run this whilst in debugging mode:

$Tagged_VM.Entity

Name                 PowerState Num CPUs MemoryGB      

----                 ---------- -------- --------      

Environment NB_LA... PoweredOn  2        8.000         

However, when I check the variable, it says "Environment NB_LAST_BACKUP Product SFC Type Server1" for example, but if I click on it, the entity name changes to the server's host name 😕

Im a little confused as to how it pulls these details and why it keeps producing what looks like columns instead of host names?

0 Kudos
LucD
Leadership
Leadership

Not sure what you mean by "...if I click on it, the entity name changes...".

In which editor/tool are you doing your debugging?


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

0 Kudos
Mike_Yazbeck
Enthusiast
Enthusiast

PowerGUI

When you debug in PowerGUI, you can view the variables in real time. It saves me a lot of time otherwise I would be sitting there using Get-Member or Select -Expandproperty every single time.

Instead I can browse the variables whilst I step over code to make sure that the output I expect and what I actually get are identical Smiley Happy

0 Kudos
LucD
Leadership
Leadership

Got it, I'm afraid I stopped using PowerGui years ago :smileyshocked:

Do you see the same happening when run it with the PS ISE?

You can set a breakpoint, hover over the variable and see the content of that variable.

In fact, the issue is still not clear to me.

Perhaps a screenshot would help?


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

0 Kudos
Mike_Yazbeck
Enthusiast
Enthusiast

What do you use these days?

When I run a brand new PowerCLI session I get the following in the attached screenshot.

EDIT: I also took the liberty of adding the PowerGUI screenshots and removed corporate data information.

0 Kudos
LucD
Leadership
Leadership

I mainly used PowerShell Plus in the past, with some PowerGui on the side.

PowerGui could never convince me, too much going on under the cover.

Since PS v4 I mainly use the PowerShell ISE, with ISESteroids in case of heavy lifting (debugging, formatting...)

Lately I'm using more and more VSCode.with the PowerShell plugin.

It;s not there yet, but I like where they are going.

On your question, do the square brackets indicate that property is an array?

Are there multiple entries under the Tag property?

All three screenshots seem to show the same thing.

The PowerGui ones show the Tag name (between brackets), followed by the Entity name.

Must be me, but I can't see the issue


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

0 Kudos
Mike_Yazbeck
Enthusiast
Enthusiast

Yes it is an array Smiley Happy

I dont undestand why the entity shows up as what I can only guess is a bunch of column names instead the entity itself - which is supposed to be the Guest VM.

When I expand the entity Array, it transforms the name from the column names to the actually Guest VM name.

I expect to see a host name not a bunch of weird column names. I dont even know where they are coming from.

0 Kudos
LucD
Leadership
Leadership

Must be a PowerGui thing :smileygrin:

Glad it confirms my decision from the past to stay away from PowerGui Smiley Wink


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

0 Kudos
Mike_Yazbeck
Enthusiast
Enthusiast

The first screenshot isnt powergui... its a PowerCLI elevated command prompt :smileysilly:

0 Kudos