Objects without .Count property - using the @ () operator (array subexpressions) versus [Array]

I'm trying to execute some simple if statements, but all new cmdlets based on [Microsoft.Management.Infrastructure.CimInstance] don't seem to open the .count method?

$Disks = Get-Disk
$Disks.Count

It does not return anything. I found that I can use this as [an array], which allows me to return the .NET.count method, as expected.

[Array]$Disks = Get-Disk
$Disks.Count

This works without directly allocating it as an array for previous cmdlets:

(Get-Services).Count

What is the recommended way to get around this?

An example that does not work:

$PageDisk = Get-Disk | Where {($_.IsBoot -eq $False) -and ($_.IsSystem -eq $False)}
  If ($PageDisk.Count -lt 1) {Write-Host "No suitable drives."; Continue}
   Else If ($PageDisk.Count -gt 1) {Write-Host "Too many drives found, manually select it."}
   Else If ($PageDisk.Count -eq 1) { Do X }

Option A (Cast as Array):

[Array]$PageDisk = Get-Disk | Where {($_.IsBoot -eq $False) -and ($_.IsSystem -eq $False)}
  If ($PageDisk.Count -lt 1) {Write-Host "No suitable drives."; Continue}
   Else If ($PageDisk.Count -gt 1) {Write-Host "Too many drives found, manually select it."}
   Else If ($PageDisk.Count -eq 1) { Do X }

Option B (Use array indices):

 $PageDisk = Get-Disk | Where {($_.IsBoot -eq $False) -and ($_.IsSystem -eq $False)}
  If ($PageDisk[0] -eq $Null) {Write-Host "No suitable drives."; Continue}
   Else If ($PageDisk[1] -ne $Null) {Write-Host "Too many drives found, manually select it."}
   Else If (($PageDisk[0] -ne $Null) -and (PageDisk[1] -eq $Null)) { Do X }

Option C (array) - Thanks @PetSerAl:

$PageDisk = @(Get-Disk | Where {($_.IsBoot -eq $False) -and ($_.IsSystem -eq $False)})
  If ($PageDisk.Count -lt 1) {Write-Host "No suitable drives."; Continue}
   Else If ($PageDisk.Count -gt 1) {Write-Host "Too many drives found, manually select it."}
   Else If ($PageDisk.Count -eq 1) { Do X }

CIM, .Count? ? B , . A , ? ?

+6
1

PSv3 + - $null - .Count (, $null, [0]).

, , .

, [pscustomobject] , , .

, [Microsoft.Management.Infrastructure.CimInstance#ROOT/Microsoft/Windows/Storage/MSFT_Disk], Get-Disk , Get-Disk - - Windows PowerShell, uservoice.com.

@(...) :

  • .

  • , .Count.


, , - , @(...), [Array] .../[object[]] ... - @() - PowerShell -, .

, , @() () , [Array] .

, @(...) [Array] ... , PetSerAl ; :

@($null) , $null, [Array] $null ( $null).

@() (. ): $null , @() ( [System.Object[]] $null ).

PetSerAl @() New-Object - - . .


@(...) :

@(), , -, , / , ( ).

, @() : PetSerAl .

  • PSv5.1 +, , , , ( ) @() :
    • , @(1, 2) 1, 2, @(, 1) , , 1.

    • , , -, System.Object[] - , (. ).
      , @( ..., ..., ...) , @() .

    • cast, [int[]]; ,
      @([int[]] (1, 2)).GetType().Name Int32[]. , @() -, System.Object[], , ; .:
      @([int[]] (1, 2))[-1] = 'foo' .
      $a = [int[]] (1, 2); $b = @([int[]] $a) - . GitHub.

  • : () @(...) , () PowerShell; -is, ; :

    • / , / [System.Object[]].

      • , @('foo').GetType().Name Object[] @('foo').Count 1 (, PSv3 +, 'foo'.Count).
        @( & { } ).Count 0 ( script " " ) ([System.Management.Automation.Internal.AutomationNull]::Value)

      • : @() New-Object, /, /, .

        • @(New-Object System.Collections.ArrayList).Count 1 - System.Object[].
        • , New-Object, (, ), , @() ( /collection ), .

        • , , , /, ():

          • @([system.collections.arraylist]::new()).Count 0; , , , @() System.Object[].
            , PSv3 + ((...)) New-Object -, New-Object - :
            @((New-Object System.Collections.ArrayList)).Count 0.
    • , (, PSv5.1 + - . ) PowerShell ([System.Object[]]):

      • , $arr = [int[]] (1, 2); @($arr) [int[]] $arr, System.Object[].
        ( , PSv5.1 +, @(), [int[]].)
+6

All Articles