Dave_Mishchenko
Immortal
Immortal

Problem with where / contains

Jump to solution

I'm trying to generate a list of VMs based on AD computers. The first example works fine and I assume because $group is a string. The second in which $group is generated by an AD query doesn't work. How do I convert $group in that case to work with -contains or is there an alternative way to structure that?

$group = "SQL02","SQL04"
get-vm | where {$group -contains $_.name}


$group = Get-ADComputer -Filter 'Name -like "SQL"' | select-object -property name
get-vm | where {$group -contains $_.name}






Dave

VMware Communities User Moderator

Now available - vSphere Quick Start Guide

Do you have a system or PCI card working with VMDirectPath? Submit your specs to the Unofficial VMDirectPath HCL.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

The Select-Object cmdlet doesn't return a string but an object. You can check by piping the output to the Get-Member cmdlet.

Get-ADComputer -Filter 'Name -like "SQL"' | select-object -property name | Get-Member

With the -contains operator you are then comparing a string against a collection of objects.

Which won't work.

You could do

$group = Get-ADComputer -Filter 'Name -like "SQL"' | %{$_.Name}
get-vm | where {$group -contains $_.name}

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership

The Select-Object cmdlet doesn't return a string but an object. You can check by piping the output to the Get-Member cmdlet.

Get-ADComputer -Filter 'Name -like "SQL"' | select-object -property name | Get-Member

With the -contains operator you are then comparing a string against a collection of objects.

Which won't work.

You could do

$group = Get-ADComputer -Filter 'Name -like "SQL"' | %{$_.Name}
get-vm | where {$group -contains $_.name}

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
Dave_Mishchenko
Immortal
Immortal

Thanks. I had tried foreach / each and while it works is seems a bit more complicated than what you've suggested.

I've come across a subsequent problem using contains when the string is null. I can get around it by checking if the count is 0, but what I'm finding odd is that if the array has a count of 1, it still returns null as in the below example.

C:\tmp>$a = "one","two","three"

C:\tmp>$a.count

3

C:\tmp>$a = "one"

C:\tmp>$a.count

C:\tmp

0 Kudos
Dave_Mishchenko
Immortal
Immortal

I found a solution here - http://www.eggheadcafe.com/software/aspnet/30677579/count-bug-in-getchilditem.aspx

Classic nuance of PowerShell. When a cmdlet only outputs a single item, you

are dealing with a scalar value. In this case a System.IO.FileInfo object

and it has no count property. As soon as you have multiple items and assign

that to a variable, you get an array. Array's in PowerShell do have a count

property. There are two ways to work with this. First is to use the

measure-object cmdlet - it handles this situation very well:

PS C:\WINDOWS\system32\windowspowershell\v1.0\x> get-childitem |

measure-object

The second way is to force the result into an array like so:

$Files = @(get-childitem)

$Files.count

1

Classic nuance of PowerShell. When a cmdlet only outputs a single item, you

are dealing with a scalar value. In this case a System.IO.FileInfo object

and it has no count property. As soon as you have multiple items and assign

that to a variable, you get an array. Array's in PowerShell do have a count

property. There are two ways to work with this. First is to use the

measure-object cmdlet - it handles this situation very well:

PS C:\WINDOWS\system32\windowspowershell\v1.0\x> get-childitem |

measure-object

The second way is to force the result into an array like so:

$Files = @(get-childitem)

$Files.count

1

>Classic nuance of PowerShell. When a cmdlet only outputs a single item, you

>are dealing with a scalar value. In this case a System.IO.FileInfo object

>and it has no count property. As soon as you have multiple items and assign

>that to a variable, you get an array. Array's in PowerShell do have a count

>property. There are two ways to work with this. First is to use the

>measure-object cmdlet - it handles this situation very well:

>PS C:\WINDOWS\system32\windowspowershell\v1.0\x> get-childitem |

>measure-object

>The second way is to force the result into an array like so:

>$Files = @(get-childitem)

>$Files.count

>1

0 Kudos
avlieshout
VMware Employee
VMware Employee

$null is treated as a scalar in PowerShell.

I always use the array sub-expression. $var = @(some code)

Also check out http://www.van-lieshout.com/2009/12/null-or-nothing/

-


Arnim van Lieshout

Blogging: http://www.van-lieshout.com

Twitter: http://www.twitter.com/avlieshout

If you find this information useful, please award points for "correct" or "helpful".

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".