Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Apparently, Get-ClusterResource exhibits nonstandard behavior by emitting an array of results as a whole (as a single object) rather than 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]

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading