Entity_Razer
Entity_Razer

Reputation: 113

Powershell Filtering with Where-Object not applying

I'm having an issue with following piece of code:

$NodeList = (get-scvmhostcluster ClusterName| where-object {$_.nodes.fullyqualifieddomainname -ne "Host001.Domain.Tld"}).nodes.fullyqualifieddomainname

Normally this should filter from the list of Hosts the "Host001.Domain.tld" out of the list. (Host001 up to Host014 is what this list normally generates, unfiltered).

However, when running the script block and seeing whats in the $NodeList variable I get every single object as if the filter was not applied.

I've been trying to debug this for a few hours now but to no avail.

Anyone able to point out my wrongdoings ?

Regards,

Upvotes: 1

Views: 1318

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 175085

Get-SCVMHostCluster ClusterName returns a single cluster object.

When you pipe it to Where-Object, you have the following:

  1. $_.Nodes is a collection of objects with a fullyqualifieddomainname property of type string
  2. $_.Nodes.fullyqualifieddomainname is therefore a collection of collections of strings

Where-Object will only collapse the first "level" of collections, it won't go deeper - and therefore your filter will never match anything, a collection of string arrays will never match the lone string you're comparing against.

Here is what I would do, collapsing the first level by selecting the Nodes property (broken into statements for readability, feel free to pipe it in one statement):

$Nodes = Get-SCVMHostCluster ClusterName|Select-Object -ExpandProperty Nodes
$NodeList = $Nodes |Where-Object {$_.fullyqualifieddomainname -ne "Host001.domain.tld"}|Select-Object -ExpandProperty fullyqualifieddomainname

Upvotes: 1

Related Questions