Get-StorageReliabilityCounter equivalent

StewartBW 1,870 Reputation points
2025-06-20T15:24:56.1333333+00:00

Hi,

These ps commands work fine, but I'm trying to get the StorageReliabilityCounter values in WinForms, and there's not a single working sample out there, do I need both MSFT_Disk and MSFT_DiskToStorageReliabilityCounter classes? If so, how to connect them together?

Any tips are highly appreciated :)

get-disk | Get-StorageReliabilityCounter | fl *

get-PhysicalDisk | Get-StorageReliabilityCounter | fl *

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,956 Reputation points
    2025-06-20T17:08:45.4766667+00:00

    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
    
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.