'Verison:        WSH 5.8
'Platform:       WinXP/Vista/WIN7
'Author:         Ken Hua (hua.ken@inventec.com.cn)
'
' Script Function:
'   Get Hard Disk S.M.A.R.T information
'

'On Error Resume Next
'Option Explicit

Const Start_Stop_Count = 4      'A tally of spindle start/stop cycles.
Const Power_On_Hours = 9        'Count of hours in power-on state.  
Const Temperature = 194          'Current internal temperature.   
Const Reallocated_Sectors_Count = 5          'Current internal temperature.  


Dim i,j,k,items(100),threshold(100),smartdata(3,11)
Dim Description(3),Limit(3),Normal(3),Bad(3),Data(3),getInfo(3)
Dim strComputer,objWMIService,colItems,objItem

strComputer = "."
getInfo(0) = Start_Stop_Count
getInfo(1) = Power_On_Hours
getInfo(2) = Temperature
getInfo(3) = Reallocated_Sectors_Count


Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\WMI")
Set colItems = objWMIService.ExecQuery("SELECT * FROM MSStorageDriver_FailurePredictThresholds",,48)
For Each objItem In colItems
  j = 0
  For i = 2 To UBound(objItem.VendorSpecific, 1) Step 12
   items(j) = objItem.VendorSpecific(i)
   threshold(j) = objItem.VendorSpecific(i+1)
                 j = j + 1
  Next
Next 

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\WMI")
Set colItems = objWMIService.ExecQuery("SELECT * FROM MSStorageDriver_ATAPISmartData",,48)
 For Each objItem In colItems      
  For i = 2 To UBound(objItem.VendorSpecific, 1) Step 12
                 If objItem.VendorSpecific(i) = getInfo(0) Then
                  k = i
                  For j = 0 To 11
                   smartdata(0,j) = objItem.VendorSpecific(k)
                   k = k + 1
                  Next 
                 End If
                 If objItem.VendorSpecific(i) = getInfo(1) Then
                  k = i
                  For j = 0 To 11
                   smartdata(1,j) = objItem.VendorSpecific(k)
                   k = k + 1
                  Next 
                 End If
                 If objItem.VendorSpecific(i) = getInfo(2) Then
                  k = i
                  For j = 0 To 11
                   smartdata(2,j) = objItem.VendorSpecific(k)
                   k = k + 1
                  Next 
                 End If
                 If objItem.VendorSpecific(i) = getInfo(3) Then
                  k = i
                  For j = 0 To 11
                   smartdata(3,j) = objItem.VendorSpecific(k)
                   k = k + 1
                  Next 
                 End If
                Next                                
 Next

 For i = 0 To 100
  If items(i) = getInfo(0) Then
   Limit(0) = threshold(i)
  End If
  If items(i) = getInfo(1) Then
   Limit(1) = threshold(i)
  End If
  If items(i) = getInfo(2) Then
   Limit(2) = threshold(i)
  End If
  If items(i) = getInfo(3) Then
   Limit(3) = threshold(i)
  End If
 Next
 
 Description(0) = "Start/Stop Count"
 Description(1) = "Power On Hours"
 Description(2) = "Temperature" 
 Description(3) = "Reallocated Sectors Count" 
  
 Normal(0) = smartdata(0,3)
 Normal(1) = smartdata(1,3)
 Normal(2) = smartdata(2,3)
 Normal(3) = smartdata(3,3)
   
 Bad(0) = smartdata(0,4)
 Bad(1) = smartdata(1,4)
 Bad(2) = smartdata(2,4)
 Bad(3) = smartdata(3,4)
 
 Data(0) = (Int(smartdata(0,7))*16*16*16)+(Int(smartdata(0,6))*16*16)+Int(smartdata(0,5))
 Data(1) = (Int(smartdata(1,7))*16*16*16)+(Int(smartdata(1,6))*16*16)+Int(smartdata(1,5))
 Data(2) = (Int(smartdata(2,7))*16*16*16)+(Int(smartdata(2,6))*16*16)+Int(smartdata(2,5))
 Data(3) = (Int(smartdata(2,7))*16*16*16)+(Int(smartdata(2,6))*16*16)+Int(smartdata(3,5))

       Wscript.echo "Hard Disk S.M.A.R.T                                 -- Hua.ken@Inventec.com.cn    " & vbCrLf _
                   &"----------------------------------------------------------------------------------" & vbCrLf _
                   &" Description(ID)   Threshold_Limit      Normal      Bad        Data               " & vbCrLf _  
                   &"----------------------------------------------------------------------------------" & vbCrLf _ 
                   & Description(0) & "(" & getInfo(0) & ")" & Space(7) & Limit(0) & Space(16) & Normal(0) & Space(12) & Bad(0) & Space(12) & Data(0) & vbCrLf _ 
                   & Description(1) & "(" & getInfo(1) & ")" & Space(7) & Limit(1) & Space(17) & Normal(1) & Space(13) & Bad(1) & Space(13) & Data(1) & vbCrLf _ 
                   & Description(2) & "(" & getInfo(2) & ")" & Space(9) & Limit(2) & Space(17) & Normal(2) & Space(13) & Bad(2) & Space(13) & Data(2) & vbCrLf _ 
                   & Description(3) & "(" & getInfo(3) & ")" & Space(9) & Limit(3) & Space(17) & Normal(3) & Space(13) & Bad(3) & Space(13) & Data(3)
Wscript.Quit