powershellfailovercluster

Why the select -first 1 doesn't work in this PowerShell command?


I'm testing in a Windows Failover cluster environment. Below is my code.

PS C:\Users\administrator.DEV> Get-ClusterResource *disk*

Name           State  OwnerGroup        ResourceType
----           -----  ----------        ------------
Cluster Disk 1 Online Available Storage Physical Disk
Cluster Disk 2 Online Cluster Group     Physical Disk
Cluster Disk 3 Online Available Storage Physical Disk


PS C:\Users\administrator.DEV> (Get-ClusterResource *disk*).GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array


PS C:\Users\administrator.DEV> (Get-ClusterResource *disk*)[0].GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    ClusterResource                          Microsoft.FailoverClusters.PowerShell.ClusterObject

PS C:\Users\administrator.DEV>

As you can see, I have three disk resource. But when I want to get the first one with the select cmdlet, I got empty output.

PS C:\Users\administrator.DEV> Get-ClusterResource *disk* | select -First 1

PS C:\Users\administrator.DEV>

Why this behavior? How can I get the first disk resource in this case?


Solution

  • Apparently, Get-ClusterResource exhibits nonstandard behavior by emitting an array of results as a whole (as a single object) rather than enumerating it, i.e. emitting its elements one by one (the latter is what cmdlets are generally expected to do).

    Therefore, either use
    (Get-ClusterResource *disk*) | select -First 1 (note the (...) to force enumeration of the array), or - as you're already showing - simply index directly into the array:
    (Get-ClusterResource *disk*)[0]