A way is with System.Management.Automation.dll
I have it at :
'C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll'
then, with help of AI :
Public Class DiskInfo
Public Property FriendlyName As String
Public Property DeviceId As String
End Class
Public Class StorageReliabilityCounter
Public Property DeviceId As String
Public Property PowerOnHours As Integer?
Public Property Temperature As Integer?
Public Property LoadUnloadCycleCount As Integer?
Public Property ReadErrorsTotal As Integer?
Public Property Wear As Integer?
' Add more properties if needed
End Class
(with System.Text.Json too...)
' Test in a Button click
Dim options As New JsonSerializerOptions With {
.PropertyNameCaseInsensitive = True
}
' Step 1: Get disk names and DeviceIds
Dim diskList As List(Of DiskInfo) = Nothing
Using psDisks = Automation.PowerShell.Create()
psDisks.AddScript("Get-PhysicalDisk | Select-Object FriendlyName, DeviceId | ConvertTo-Json")
Dim diskResults = psDisks.Invoke()
If psDisks.HadErrors OrElse diskResults.Count = 0 Then
Debug.WriteLine("Error retrieving physical disks or no results")
Return
End If
Dim diskJson = diskResults(0).ToString()
diskList = JsonSerializer.Deserialize(Of List(Of DiskInfo))(diskJson, options)
End Using
' Step 2: Get reliability counters
Dim counterList As List(Of StorageReliabilityCounter) = Nothing
Using psCounters = Automation.PowerShell.Create()
psCounters.AddScript("Get-PhysicalDisk | Get-StorageReliabilityCounter | ConvertTo-Json -Depth 5")
Dim counterResults = psCounters.Invoke()
If psCounters.HadErrors OrElse counterResults.Count = 0 Then
Debug.WriteLine("Error retrieving storage reliability counters or no results")
Return
End If
Dim counterJson = counterResults(0).ToString()
counterList = JsonSerializer.Deserialize(Of List(Of StorageReliabilityCounter))(counterJson, options)
End Using
' Step 3: Join and display
For Each counter In counterList
Dim diskName = diskList.FirstOrDefault(Function(d) d.DeviceId = counter.DeviceId)?.FriendlyName
Debug.WriteLine($"Disk: {If(diskName, "Unknown")}")
Debug.WriteLine($" PowerOnHours: {counter.PowerOnHours}")
Debug.WriteLine($" Temperature: {counter.Temperature}")
Debug.WriteLine($" LoadUnloadCycleCount: {counter.LoadUnloadCycleCount}")
Debug.WriteLine($" ReadErrorsTotal: {counter.ReadErrorsTotal}")
Debug.WriteLine($" Wear: {counter.Wear}")
Debug.WriteLine(New String("-"c, 20))
Next