如何在不使用Python中的WMI的情况下获取PhysicalDisk?

2024-04-29 08:44:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我想要物理磁盘。我需要的信息之一是BusType。我想在不使用WMI的情况下找到它。有办法吗

谢谢你的建议和信息


Tags: 信息物理情况wmi建议磁盘办法bustype
1条回答
网友
1楼 · 发布于 2024-04-29 08:44:23

您可以使用powershell获取物理磁盘的总线类型:

参考:https://www.action1.com/kb/getting-PC-hard-drive-information-using-Powershell.html

Get-PhysicalDisk

输出:

Number FriendlyName        SerialNumber                             MediaType CanPool OperationalStatus HealthStatus Usage            Size
                                                         -    -         -          -              
0      INTEL SSDPEKNW512G8 0000_0000_0100_0000_E4D2_XXXX_XXXX_XXXX. SSD       False   OK                Healthy      Auto-Select 476.94 GB

要获取总线类型:

Get-PhysicalDisk | ft -AutoSize DeviceId,Model,MediaType,BusType,Size

输出:

DeviceId Model               MediaType BusType         Size
       -                   -    -           
0        INTEL SSDPEKNW512G8 SSD       NVMe    512110190592

要从python调用powershell,请执行以下操作:

参考:https://www.phillipsj.net/posts/executing-powershell-from-python/

completed = subprocess.run(["powershell", "-Command", "Get-PhysicalDisk | ft -AutoSize DeviceId,Model,MediaType,BusType,Size"], capture_output=True)

例如:

C:\Users\XXXXXX>python3
Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:20:19) [MSC v.1925 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> ps_command = "Get-PhysicalDisk | ft -AutoSize DeviceId,Model,MediaType,BusType,Size"
>>> completed = subprocess.run(["powershell", "-Command", ps_command], capture_output=True)
>>> print(completed)
CompletedProcess(args=['powershell', '-Command', 'Get-PhysicalDisk | ft -AutoSize DeviceId,Model,MediaType,BusType,Size'], returncode=0, stdout=b'\r\nDeviceId Model               MediaType BusType         Size\r\n       -                   -    -           \r\n0        INTEL SSDPEKNW512G8 SSD       NVMe    512110190592\r\n\r\n\r\n', stderr=b'')
>>>

因此,您可以制作这样的脚本:


# get_disk_bustype.py
import subprocess
ps_command = "Get-PhysicalDisk | ft -AutoSize BusType"

run_result = subprocess.run(["powershell", "-Command", ps_command], capture_output=True)
print(run_result)
# CompletedProcess(args=['powershell', '-Command', 'Get-PhysicalDisk | ft -AutoSize BusType'], returncode=0, stdout=b'\r\nBusType\r\n   -\r\nNVMe   \r\n\r\n\r\n', stderr=b'')

run_result_stdout = str(run_result.stdout)
print(run_result_stdout)
# '\r\nBusType\r\n   -\r\nNVMe   \r\n\r\n\r\n'

run_result_stdout_bustype = run_result_stdout.split("\\r\\n")[3]
print(run_result_stdout_bustype)
# '0        NVMe   '

run_result_stdout_bustype_clean = run_result_stdout_bustype.strip(" ")
print(run_result_stdout_bustype_clean)
# 'NVMe'


输出:

C:\Users\XXXXX>python3 get_disk_bustype.py
CompletedProcess(args=['powershell', '-Command', 'Get-PhysicalDisk | ft -AutoSize BusType'], returncode=0, stdout=b'\r\nBusType\r\n   -\r\nNVMe   \r\n\r\n\r\n', stderr=b'')
b'\r\nBusType\r\n   -\r\nNVMe   \r\n\r\n\r\n'
NVMe
NVMe

相关问题 更多 >