为什么这个 powershell 排序不起作用?(物理磁盘对象)

为什么这个 powershell 排序不起作用?(物理磁盘对象)
PS C:\WINDOWS\system32> Get-PhysicalDisk | Sort-Object Number

Number FriendlyName           SerialNumber MediaType   CanPool OperationalStatus HealthStatus Usage            Size
------ ------------           ------------ ---------   ------- ----------------- ------------ -----            ----
3      Seagate Backup+  Desk  NA7E5JCS     Unspecified False   OK                Healthy      Auto-Select   2.73 TB
0      Crucial_CT500MX200SSD1 162112CAE0AE SSD         False   OK                Healthy      Auto-Select 465.76 GB
2      Seagate Expansion Desk NA4K7L4K     Unspecified False   OK                Healthy      Auto-Select   3.64 TB


PS C:\WINDOWS\system32> $psversionTable

Name                           Value
----                           -----
PSVersion                      5.1.19041.610
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.610
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

答案1

为什么这个 PowerShell 排序不起作用?

Get-PhysicalDisk | Sort-Object Number

看起来你的语法不正确。

Get-PhysicalDisk | Sort-Object -Property DeviceID -Descending
Get-PhysicalDisk | Sort-Object -Property DeviceID

输出:

Number FriendlyName            SerialNumber         MediaType CanPool OperationalStatus HealthStatus Usage            Size
    ------ ------------            ------------         --------- ------- ----------------- ------------ -----            ----
    5      MSFT XVDD                                    SSD       False   OK                Healthy      Auto-Select  29.38 GB
    4      MSFT XVDD                                    SSD       False   OK                Healthy      Auto-Select 109.91 GB
    3      MSFT XVDD                                    SSD       False   OK                Healthy      Auto-Select  11.34 GB
    2      ST10000NM0086                                HDD       True    OK                Healthy      Auto-Select    9.1 TB
    1      Samsung SSD 860 EVO 2TB                      SSD       False   OK                Healthy      Auto-Select   1.82 TB
    0      Samsung SSD 970 PRO 1TB                      SSD       False   OK                Healthy      Auto-Select 953.87 GB

Number FriendlyName            SerialNumber         MediaType CanPool OperationalStatus HealthStatus Usage            Size
    ------ ------------            ------------         --------- ------- ----------------- ------------ -----            ----
    0      Samsung SSD 970 PRO 1TB                      SSD       False   OK                Healthy      Auto-Select 953.87 GB
    1      Samsung SSD 860 EVO 2TB                      SSD       False   OK                Healthy      Auto-Select   1.82 TB
    2      ST10000NM0086                                HDD       True    OK                Healthy      Auto-Select    9.1 TB
    3      MSFT XVDD                                    SSD       False   OK                Healthy      Auto-Select  11.34 GB
    4      MSFT XVDD                                    SSD       False   OK                Healthy      Auto-Select 109.91 GB
    5      MSFT XVDD                                    SSD       False   OK                Healthy      Auto-Select  29.38 GB

来源:示例 1:按名称对当前目录进行排序

获取物理磁盘Number实际上并没有返回如下命令所明确的属性:

 Get-PhysicalDisk | Select-Object Number, FriendlyName, Size

Number FriendlyName                      Size
------ ------------                      ----
       MSFT XVDD                  12179415040
       MSFT XVDD                 118010777600
       MSFT XVDD                  31548567552
       Samsung SSD 970 PRO 1TB  1024209543168
       ST10000NM0086            10000831348736
       Samsung SSD 860 EVO 2TB  2000398934016

而以下命令返回正确的输出:

Get-PhysicalDisk | Select-Object DeviceID, FriendlyName,Size

DeviceID FriendlyName                      Size
-------- ------------                      ----
3        MSFT XVDD                  12179415040
4        MSFT XVDD                 118010777600
5        MSFT XVDD                  31548567552
0        Samsung SSD 970 PRO 1TB  1024209543168
2        ST10000NM0086           10000831348736
1        Samsung SSD 860 EVO 2TB  2000398934016

答案2

始终使用 Get-Member 来确保获取原始属性与我们都知道的屏幕上显示的内容经常被操纵相比。

(Get-PhysicalDisk)[0] | 
Get-Member
# Results
<#
   TypeName: Microsoft.Management.Infrastructure.CimInstance#root/microsoft/windows/storage/MSFT_PhysicalDisk

Name                             MemberType     Definition                                                                                                                        
----                             ----------     ----------                                                                                                                        
...                                                                                                                
AdapterSerialNumber              Property       string AdapterSerialNumber {get;}                                                                                                 
AllocatedSize                    Property       uint64 AllocatedSize {get;}                                                                                                       
CanPool                          Property       bool CanPool {get;}                                                                                                               
Description                      Property       string Description {get;}                                                                                                         
DeviceId                         Property       string DeviceId {get;}                                                                                                            
EnclosureNumber                  Property       uint16 EnclosureNumber {get;}                                                                                                     
FirmwareVersion                  Property       string FirmwareVersion {get;}                                                                                                     
FriendlyName                     Property       string FriendlyName {get;}                                                                                                        
IsIndicationEnabled              Property       bool IsIndicationEnabled {get;}                                                                                                   
IsPartial                        Property       bool IsPartial {get;}                                                                                                             
LogicalSectorSize                Property       uint64 LogicalSectorSize {get;}                                                                                                   
Manufacturer                     Property       string Manufacturer {get;}                                                                                                        
Model                            Property       string Model {get;}                                                                                                               
ObjectId                         Property       string ObjectId {get;}                                                                                                            
OperationalDetails               Property       string[] OperationalDetails {get;}                                                                                                
OtherCannotPoolReasonDescription Property       string OtherCannotPoolReasonDescription {get;}                                                                                    
PartNumber                       Property       string PartNumber {get;}                                                                                                          
PassThroughClass                 Property       string PassThroughClass {get;}                                                                                                    
PassThroughIds                   Property       string PassThroughIds {get;}                                                                                                      
PassThroughNamespace             Property       string PassThroughNamespace {get;}                                                                                                
PassThroughServer                Property       string PassThroughServer {get;}                                                                                                   
PhysicalLocation                 Property       string PhysicalLocation {get;}                                                                                                    
PhysicalSectorSize               Property       uint64 PhysicalSectorSize {get;}                                                                                                  
PSComputerName                   Property       string PSComputerName {get;}                                                                                                      
SerialNumber                     Property       string SerialNumber {get;}                                                                                                        
Size                             Property       uint64 Size {get;}                                                                                                                
SlotNumber                       Property       uint16 SlotNumber {get;}                                                                                                          
SoftwareVersion                  Property       string SoftwareVersion {get;}                                                                                                     
StoragePoolUniqueId              Property       string StoragePoolUniqueId {get;}                                                                                                 
UniqueId                         Property       string UniqueId {get;}                                                                                                            
...
#>

显然,'Number'是DeviceId的字符串重写。

答案3

尽管表面上如此,但 Get-PhysicalDisk cmdlet 返回的对象并没有“Number”属性。此列标题似乎被放在名为 DeviceID 的属性上。此外,此 ID 是一个字符串,因此需要将其转换为数字才能正确排序。

所以这应该可以工作,但我没有在多磁盘机器上验证它:

PS C:\WINDOWS\system32>  Get-PhysicalDisk | Sort-Object {[int]$_.DeviceID}

Number FriendlyName           SerialNumber MediaType   CanPool OperationalStatus HealthStatus Usage            Size
------ ------------           ------------ ---------   ------- ----------------- ------------ -----            ----
0      Crucial_CT500MX200SSD1 162112CAE0AE SSD         False   OK                Healthy      Auto-Select 465.76 GB
2      Seagate Expansion Desk NA4K7L4K     Unspecified False   OK                Healthy      Auto-Select   3.64 TB
3      Seagate Backup+  Desk  NA7E5JCS     Unspecified False   OK                Healthy      Auto-Select   2.73 TB

感谢@Jazimov 发现我之前的解决方案中存在一个数据依赖的问题。现在我明白了,字符串与数字排序是有意义的。这不完全是一个错误,但它非常违反直觉。

相关内容