Cheries
Cheries

Reputation: 892

How to get PhysicalDisk without using WMI in Python?

I would like to get the Physical Disk. One of the information that I need is BusType. I would like to to find it without using WMI. Is there any way to do that?

Thank you for the advice and information.

Upvotes: 0

Views: 944

Answers (1)

Kristian
Kristian

Reputation: 2505

You can get bus type of physical disk with powershell:

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

Get-PhysicalDisk

output:

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

To get bus type:

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

output:

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

To call powershell from python:

Ref: 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)

example:

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'')
>>>

Therefore you can make such script:


# 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'


output:

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

Upvotes: 2

Related Questions