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

page last uploaded: 2016-12-15, 11:20