end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

power shellでwindowsのdiskの健康状態(smart?)を取得 (故障の予兆管理)

目次

Get-PhysicalDisk

S.M.A.R.T情報を取得しているんですかね

PS1> Get-PhysicalDisk | `
       Select-Object FriendlyName,MediaType,OperationalStatus,HealthStatus

FriendlyName            MediaType OperationalStatus HealthStatus
------------            --------- ----------------- ------------
SKHynix_HFS512GDE9X081N SSD       OK                Healthy

OperationalStatus - 物理ディスクの動作状態

意味
Unknown 状態が不明
OK 正常に動作
Degraded いくつかの問題が検出
Pred Fail 故障が予測される
Error エラーが発生
Non-Recover 障害が発生し、回復不可
Starting 開始処理が進行中
Stopping 停止処理が進行中
Service サービスが停止
No Contact 接触がない

HealthStatus - 物理ディスクの健康状態

意味
Healthy 正常な状態
Warning いくつかの問題が検出
Unhealthy エラーまたは障害が発生
Unknown 状態が不明

Get-PhysicalDisk の結果をcsvに出力

$folderPath = "c:/Users/end0t/tmp"
$fileName = "{0}_{1}_pc_health.csv" -f (Get-Date).ToString("yyyyMM"), $Env:USERNAME
$filePath = Join-Path -Path $folderPath -ChildPath $fileName

$date = Get-Date -Format "yyyyMMdd"
$hour = Get-Date -Format "HH"
$username     = $env:USERNAME
$computername = $env:COMPUTERNAME

# CSVファイルに追記するためのプロパティを定義する
$properties = @(
    @{Name="Date"             ; Expression={$date}},
    @{Name="Hour"             ; Expression={$hour}},
    @{Name="Username"         ; Expression={$username}},
    @{Name="Computername"     ; Expression={$computername}},
    @{Name="FriendlyName"     ; Expression={$_.FriendlyName}},
    @{Name="MediaType"        ; Expression={$_.MediaType}},
    @{Name="OperationalStatus"; Expression={$_.OperationalStatus}},
    @{Name="HealthStatus"     ; Expression={$_.HealthStatus}}
)

# Get-PhysicalDiskコマンドレットを実行し、CSVファイルに結果を追記する
Get-PhysicalDisk | Select-Object $properties | `
  Export-Csv -Path $filePath -Encoding UTF8 -Append -NoTypeInformation
"Date","Hour","Username","Computername","FriendlyName","MediaType","OperationalStatus","HealthStatus"
"20230429","06","end0t","LAPTOP-FSIBOAPD","SKHynix_HFS512GDE9X081N","SSD","OK","Healthy"

Get-PhysicalDisk | Get-StorageReliabilityCounter | FL

※以下の実行には、windowsの管理者権限が必要です

PS1> Get-PhysicalDisk | Get-StorageReliabilityCounter | FL

ObjectId                : {1}\\LAPTOP-FSIBOAPD\root/Micr<略>
PassThroughClass        :
PassThroughIds          :
PassThroughNamespace    :
PassThroughServer       :
UniqueId                : {9edd8bdd-6752-11eb-8357-806e6f6e6963}:<略>
DeviceId                : 0
FlushLatencyMax         : 740
LoadUnloadCycleCount    :
LoadUnloadCycleCountMax :
ManufactureDate         :
PowerOnHours            :
ReadErrorsCorrected     :
ReadErrorsTotal         :
ReadErrorsUncorrected   :
ReadLatencyMax          : 4682
StartStopCycleCount     :
StartStopCycleCountMax  :
Temperature             : 37
TemperatureMax          : 83
Wear                    : 0
WriteErrorsCorrected    :
WriteErrorsTotal        :
WriteErrorsUncorrected  :
WriteLatencyMax         : 852
PSComputerName          :

Get-WmiObject

以下のように表示されますが、活用方法が不明です。

> Get-WmiObject -Class Win32_PerfFormattedData_PerfDisk_PhysicalDisk

__GENUS                 : 2
__CLASS                 : Win32_PerfFormattedData_PerfDisk_PhysicalDisk
__SUPERCLASS            : Win32_PerfFormattedData
__DYNASTY               : CIM_StatisticalInformation
__RELPATH               : Win32_PerfFormattedData_PerfDisk_PhysicalDisk.Name="0 C:"
__PROPERTY_COUNT        : 30
__DERIVATION            : {Win32_PerfFormattedData, Win32_Perf, CIM_StatisticalInformation}
__SERVER                : LAPTOP-FSIBOAPD
__NAMESPACE             : root\cimv2
__PATH                  : \\LAPTOP-FSIBOAPD\root\cimv2:Win32_PerfFormattedData_PerfDisk_PhysicalDisk.Name="0 C:"
AvgDiskBytesPerRead     : 0
AvgDiskBytesPerTransfer : 0
AvgDiskBytesPerWrite    : 0
AvgDiskQueueLength      : 0
AvgDiskReadQueueLength  : 0
AvgDisksecPerRead       : 0
AvgDisksecPerTransfer   : 0
AvgDisksecPerWrite      : 0
AvgDiskWriteQueueLength : 0
Caption                 :
CurrentDiskQueueLength  : 0
Description             :
DiskBytesPersec         : 0
DiskReadBytesPersec     : 0
DiskReadsPersec         : 0
DiskTransfersPersec     : 0
DiskWriteBytesPersec    : 0
DiskWritesPersec        : 0
Frequency_Object        :
Frequency_PerfTime      :
Frequency_Sys100NS      :
Name                    : 0 C:
PercentDiskReadTime     : 0
PercentDiskTime         : 0
PercentDiskWriteTime    : 0
PercentIdleTime         : 98
SplitIOPerSec           : 0
Timestamp_Object        :
Timestamp_PerfTime      :
Timestamp_Sys100NS      :
PSComputerName          : LAPTOP-FSIBOAPD

__GENUS                 : 2
__CLASS                 : Win32_PerfFormattedData_PerfDisk_PhysicalDisk
__SUPERCLASS            : Win32_PerfFormattedData
__DYNASTY               : CIM_StatisticalInformation
__RELPATH               : Win32_PerfFormattedData_PerfDisk_PhysicalDisk.Name="_Total"
__PROPERTY_COUNT        : 30
__DERIVATION            : {Win32_PerfFormattedData, Win32_Perf, CIM_StatisticalInformation}
__SERVER                : LAPTOP-FSIBOAPD
__NAMESPACE             : root\cimv2
__PATH                  : \\LAPTOP-FSIBOAPD\root\cimv2:Win32_PerfFormattedData_PerfDisk_PhysicalDisk.Name="_Total"
AvgDiskBytesPerRead     : 0
AvgDiskBytesPerTransfer : 0
AvgDiskBytesPerWrite    : 0
AvgDiskQueueLength      : 0
AvgDiskReadQueueLength  : 0
AvgDisksecPerRead       : 0
AvgDisksecPerTransfer   : 0
AvgDisksecPerWrite      : 0
AvgDiskWriteQueueLength : 0
Caption                 :
CurrentDiskQueueLength  : 0
Description             :
DiskBytesPersec         : 0
DiskReadBytesPersec     : 0
DiskReadsPersec         : 0
DiskTransfersPersec     : 0
DiskWriteBytesPersec    : 0
DiskWritesPersec        : 0
Frequency_Object        :
Frequency_PerfTime      :
Frequency_Sys100NS      :
Name                    : _Total
PercentDiskReadTime     : 0
PercentDiskTime         : 0
PercentDiskWriteTime    : 0
PercentIdleTime         : 98
SplitIOPerSec           : 0
Timestamp_Object        :
Timestamp_PerfTime      :
Timestamp_Sys100NS      :
PSComputerName          : LAPTOP-FSIBOAPD