how to check if hard drive is eide or sata with powershell

北战南征 提交于 2019-12-23 03:38:10

问题


I like to know if there is any win32 class that can detect if a hard drive is eide or sata. Thanks in advance.


回答1:


There is no straight way to find that. However, you can use the caption property of Win32_DiskDrive and parse it to see if you have a ATA or SCSI disk. On my system, SATA disk has a caption ST9500420AS ATA Device.

The way you do this is:

Get-WMIObject -Class Win32_DiskDrive | Select Caption, Index

You can parse the Caption property to find if it contains ATA or SCSI.




回答2:


As noted in the other answer the drive's caption (ie. model name) might include this information you can navigate the WMI objects associations until you get to a device with a more definitive name/caption/other property.

Keeping devmgmt.msc open with View | Devices by Connection open while developing will make things easier.

The key to navigating the WMI object graph is "ASSOCIATORS OF" queries.

So (using lots of aliases and other shortcuts to make this easier: I would avoid this in something I plan to reuse):

gwmi win32_DiskDrive |
  %{gwmi -query "ASSOCIATORS OF {$($_.__RELPATH)} where resultclass = Win32_PnpEntity"}

will get the Win32_PnpEntity objects for each disk drive.

Repeating this on the first (for the purposes of exploration) disk drive to another level to find what kind of associations exist:

gwmi win32_DiskDrive |
  %{gwmi -query "ASSOCIATORS OF {$($_.__RELPATH)} where resultclass = Win32_PnpEntity" |
    %{gwmi -query "ASSOCIATORS OF {$($_.__RELPATH)}"}} | fl __CLASS,__RELPATH

shows a mix of WMI classes:

__CLASS   : Win32_SystemDriver
__RELPATH : Win32_SystemDriver.Name="disk"

__CLASS   : Win32_ComputerSystem
__RELPATH : Win32_ComputerSystem.Name="hostname"

__CLASS   : Win32_IDEController
__RELPATH : Win32_IDEController.DeviceID="PCIIDE\\IDECHANNEL\\4&5ECF4F&0&2"

__CLASS   : CIM_DataFile
__RELPATH : CIM_DataFile.Name="c:\\windows\\system32\\drivers\\disk.sys"

__CLASS   : Win32_DiskDrive
__RELPATH : Win32_DiskDrive.DeviceID="\\\\.\\PHYSICALDRIVE0"

The last of these is just navigating back to the disk drive, and every device is associated with a computer system. But that Win32_IDEController object looks interesting.

It had a ProtocolSupported property with values for different buses, but all instances here are 37 ("IDE"), and there are instances for both the controller channels and the controllers:

PS[64bit] C:\bin\PowerShell> gwmi win32_idecontroller | ft -auto -wrap caption,description

caption                                  description
-------                                  -----------
ATA Channel 1                            IDE Channel
ATA Channel 0                            IDE Channel
ATA Channel 1                            IDE Channel
Standard AHCI 1.0 Serial ATA Controller  Standard AHCI 1.0 Serial ATA Controller
Standard AHCI 1.0 Serial ATA Controller  Standard AHCI 1.0 Serial ATA Controller
Standard Dual Channel PCI IDE Controller Standard Dual Channel PCI IDE Controller
ATA Channel 0                            IDE Channel
ATA Channel 1                            IDE Channel
ATA Channel 2                            IDE Channel
ATA Channel 3                            IDE Channel
ATA Channel 4                            IDE Channel
ATA Channel 5                            IDE Channel
ATA Channel 0                            IDE Channel

So it isn't going to be as easy as getting to a Win32_IDEController.

Going back and expanding the Win32_IDEController associated with Win32_PnpDevice for my disk:

gwmi win32_DiskDrive |
  %{gwmi -query "ASSOCIATORS OF {$($_.__RELPATH)} where resultclass = Win32_PnpEntity" |
    %{gwmi -query "ASSOCIATORS OF {$($_.__RELPATH)}  where resultclass = Win32_IDEController"}} |
  fl Caption,Description
caption     : ATA Channel 2
description : IDE Channel

So that's the SATA channel, will the channel be associated with the controller? And simplifying: the output of a foreach-object doesn't need an inner pipeline:

gwmi win32_DiskDrive |
  %{gwmi -query "ASSOCIATORS OF {$($_.__RELPATH)} where resultclass = Win32_PnpEntity"} |
  %{gwmi -query "ASSOCIATORS OF {$($_.__RELPATH)}  where resultclass = Win32_IDEController"} |
  %{gwmi -query "ASSOCIATORS OF {$($_.__RELPATH)}  where resultclass = Win32_IDEController"} |
  fl __CLASS,__RELPATH.Caption,Description

This finds nothing, but a little exploring (capturing the __RELPATH from one query to paste into another to keep pipeline under control) indicates that following the associations:

DiskDrive --> PnpDevice --> IDEContoller --> PnpDevice --> IDEController

should get the result. Avoiding looping (as the IDEController has two associated PnpDevice objects) is left as an exercise.

Once the true controller is reached, the caption will need to be parsed.

The Bad News

Repeating the query for controllers on another system gave three instances of Standard Dual Channel PCI IDE Controller despite having four controller (1×IDE and 3×SATA), this might be related to the non-trivial mapping due to one running as RAID?

And of course SATA has essentially the same logical (programming) interface as IDE to make new hardware work with software (the same applies with PCI and PCI-Express) means the OS doesn't really need to know.



来源:https://stackoverflow.com/questions/5362199/how-to-check-if-hard-drive-is-eide-or-sata-with-powershell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!