Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for gethddstatus.ps

(view source code of gethddstatus.ps as plain text)

  1. param(
  2. 	[parameter( ValueFromRemainingArguments = $true )]
  3. 	[string[]]$Args # Leave all argument validation to the script, not to PowerShell
  4. )
  5.  
  6. Clear-Host
  7. Write-Host
  8.  
  9. if ( $args.Length -gt 0 ) {
  10. 	#
  11. 	# GetHDDStatus.ps1,  Version 1.02
  12. 	# Get the SMART status for all local harddisks
  13. 	#
  14. 	# Usage:    powershell  ./GetHDDStatus.ps1
  15. 	#
  16. 	# Notes:    This script requires elevated privileges.
  17. 	#           In Linux, the output does not include disk indexes.
  18. 	#
  19. 	# Credits:  Windows part based on code by Geoff @ UVM:
  20. 	#           www.uvm.edu/~gcd/2013/01/which-disk-is-that-volume-on
  21. 	#
  22. 	# Written by Rob van der Woude
  23. 	# http://www.robvanderwoude.com
  24. 	#
  25.  
  26. 	Write-Host "GetHDDStatus.ps1,  Version 1.02"
  27. 	Write-Host "Get the SMART status for all local harddisks"
  28. 	Write-Host
  29. 	Write-Host "Usage:    " -NoNewline
  30. 	Write-Host "powershell  ./GetHDDStatus.ps1" -ForegroundColor White
  31. 	Write-Host
  32. 	Write-Host "Notes:    This script requires elevated privileges."
  33. 	Write-Host "          In Linux, the output does not include disk indexes."
  34. 	Write-Host
  35. 	Write-Host "Credits:  Windows part based on code by Geoff @ UVM:"
  36. 	Write-Host "          www.uvm.edu/~gcd/2013/01/which-disk-is-that-volume-on" -ForegroundColor DarkGray
  37. 	Write-Host
  38. 	Write-Host "Written by Rob van der Woude"
  39. 	Write-Host "http://www.robvanderwoude.com"
  40. 	Write-Host
  41.  
  42. 	exit 1
  43. }
  44.  
  45. $rc = 0
  46.  
  47. if ( $HOME[0] -eq '/' ) {
  48. 	# Linux: lshw/smartctl/df commands
  49.  
  50. 	Write-Host "Volume   `tStatus`tCapacity`tModel" -ForegroundColor White
  51. 	Write-Host "======   `t======`t========`t=====`n" -ForegroundColor White
  52.  
  53. 	( . lshw -short -class disk ) -match "/dev/" | ForEach-Object {
  54. 		$disk = ( $_.trim( ) -split '\s+',  3 )[1]
  55. 		$name = ( $_.trim( ) -split 'disk', 2 )[1].trim( )
  56. 		if ( $disk -notmatch "(/cd|/dvd|/sr)" ) {
  57. 			try {
  58. 				$size = 0
  59. 				$size = ( ( ( . df -l --output=source,size ) -match $disk ) -split '\s+', 2 )[1] / 1MB
  60. 				if ( $size -gt 0 ) {
  61. 					$test = ( ( ( . smartctl -H $disk ) -match "SMART [^\n\r]+: ([A-Z]+)" ) -split ":" )[1].trim( )
  62. 					Write-Host "$disk`t" -ForegroundColor White -NoNewline
  63. 					if ( $test -eq "PASSED" ) {
  64. 						$fgc = "Green"
  65. 					} else {
  66. 						$fgc = "Red"
  67. 						$rc = 1
  68. 					}
  69. 					Write-Host "$test`t" -ForegroundColor $fgc -NoNewline
  70. 					Write-Host ( "{0,5:N0} GB`t$name" -f $size ) -ForegroundColor White
  71. 				}
  72. 			}
  73. 			catch {
  74. 				# ignore errors from USB sticks etc.
  75. 			}
  76. 		}
  77. 	}
  78. } else {
  79. 	# Windows: WMI
  80.  
  81. 	# based on code by Geoff @ UVM
  82. 	# https://www.uvm.edu/~gcd/2013/01/which-disk-is-that-volume-on/
  83.  
  84. 	[System.Collections.SortedList]$volumedetails = New-Object System.Collections.SortedList
  85. 	[System.Collections.SortedList]$volumestatus  = New-Object System.Collections.SortedList
  86.  
  87. 	$diskdrives = Get-WmiObject -Namespace "root/CIMV2" -Class Win32_DiskDrive
  88. 	foreach ( $disk in $diskdrives ) {
  89. 		$diskindex  = $disk.Index
  90. 		$diskmodel  = $disk.Model
  91. 		$disksize   = "{0,5:F0} GB" -f ( $disk.Size / 1GB )
  92. 		$diskstatus = $disk.Status
  93. 		$part_query = 'ASSOCIATORS OF {Win32_DiskDrive.DeviceID="' + $disk.DeviceID.replace('\','\\') + '"} WHERE AssocClass=Win32_DiskDriveToDiskPartition'
  94. 		$partitions = @( Get-WmiObject -Query $part_query | Sort-Object StartingOffset )
  95. 		foreach ( $partition in $partitions ) {
  96. 			$vol_query = 'ASSOCIATORS OF {Win32_DiskPartition.DeviceID="' + $partition.DeviceID + '"} WHERE AssocClass=Win32_LogicalDiskToPartition'
  97. 			$volumes   = @( Get-WmiObject -Query $vol_query )
  98. 			foreach ( $volume in $volumes ) {
  99. 				# DriveType 3 means harddisks only
  100. 				# 0 = Unknown; 1 = No Root Directory; 2 = Removable Disk; 3 = Local Disk; 4 = Network Drive; 5 = Compact Disc; 6 = RAM Disk
  101. 				if ( $volume.DriveType -eq 3 ) {
  102. 					if ( -not $volumedetails.Contains( $volume.Name ) ) {
  103. 						$volumedetails.Add( $volume.Name, "[Disk $diskindex]  $disksize  $diskmodel" )
  104. 						$volumestatus.Add( $volume.Name, $diskstatus )
  105. 					}
  106. 				}
  107. 			}
  108. 		}
  109. 	}
  110.  
  111. 	Write-Host "Volume  Status  Disk#     Capacity  Model" -ForegroundColor White
  112. 	Write-Host "======  ======  =====     ========  =====`n" -ForegroundColor White
  113.  
  114. 	$volumedetails.Keys | ForEach-Object {
  115. 		$fgc = "Green"
  116. 		$status = ( $volumestatus[$_] )
  117. 		if ( $status -ne "OK" ) {
  118. 			$fgc = "Red"
  119. 			$rc  = 1
  120. 		}
  121. 		Write-Host ( "$_      " ) -ForegroundColor White -NoNewline
  122. 		Write-Host ( "$status      ".Substring( 0, 6 ) + "  " ) -ForegroundColor $fgc -NoNewline
  123. 		Write-Host ( $volumedetails[$_] ) -ForegroundColor White
  124. 	}
  125. }
  126.  
  127. Write-Host
  128.  
  129. exit $rc
  130.  

page last uploaded: 2017-08-21, 14:26