Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for gethddstatusgui.ps

(view source code of gethddstatusgui.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.  
  7. ########
  8. # Help #
  9. ########
  10.  
  11. if ( ( $Args.Length -gt 0 ) -or ( $HOME[0] -eq '/' ) ) {
  12. 	Clear-Host
  13. 	Write-Host
  14. 	if ( $HOME[0] -eq '/' ) {
  15. 		# Linux
  16. 		Write-Host "GetHDDStatusGUI.ps1 is for Windows only"
  17. 		Write-Host "Use GetHDDStatus.ps1 instead for Linux"
  18. 		Write-Host
  19. 	}
  20. 	Write-Host "GetHDDStatusGUI.ps1,  Version 1.01"
  21. 	Write-Host "Get the SMART status for all local harddisks"
  22. 	Write-Host
  23. 	Write-Host "Usage:    " -NoNewline
  24. 	Write-Host "powershell  . GetHDDStatusGUI.ps1" -ForegroundColor White
  25. 	Write-Host
  26. 	Write-Host "Notes:    Disks' status is shown in a dialog and as desktop notification."
  27. 	Write-Host "          This script does " -NoNewline
  28. 	Write-Host "not" -NoNewline -ForegroundColor White
  29. 	Write-Host " require elevated privileges."
  30. 	Write-Host "          When started, this script will minimize the console window;"
  31. 	Write-Host "          when finished, the console window will be restored, but minimized."
  32. 	Write-Host
  33. 	Write-Host "Credits:  Disk check based on code by Geoff:"
  34. 	Write-Host "          www.uvm.edu/~gcd/2013/01/which-disk-is-that-volume-on" -ForegroundColor DarkGray
  35. 	Write-Host "          System Tray ToolTip Balloon code by Don Jones:"
  36. 	Write-Host "          http://blog.sapien.com/current/2007/4/27" -ForegroundColor DarkGray
  37. 	Write-Host "          /creating-a-balloon-tip-notification-in-powershell.html" -ForegroundColor DarkGray
  38. 	Write-Host "          Code to extract icons from Shell32.dll by Thomas Levesque:"
  39. 	Write-Host "          http://stackoverflow.com/questions/6873026" -ForegroundColor DarkGray
  40. 	Write-Host "          Code to hide and restore console by Anthony:"
  41. 	Write-Host "          http://stackoverflow.com/a/15079092" -ForegroundColor DarkGray
  42. 	Write-Host
  43. 	Write-Host "Written by Rob van der Woude"
  44. 	Write-Host "http://www.robvanderwoude.com"
  45. 	Write-Host
  46.  
  47. 	exit 1
  48. }
  49.  
  50.  
  51. #######################################
  52. # Hide console window                 #
  53. # by Anthony on StackOverflow.com     #
  54. # http://stackoverflow.com/a/15079092 #
  55. #######################################
  56.  
  57. $signature1 = @'
  58. public static void ShowConsoleWindow( int state )
  59. {
  60. 	var handle = GetConsoleWindow( );
  61. 	ShowWindow( handle, state );
  62. }
  63.  
  64. [System.Runtime.InteropServices.DllImport( "kernel32.dll" )]
  65. static extern IntPtr GetConsoleWindow( );
  66.  
  67. [System.Runtime.InteropServices.DllImport( "user32.dll" )]
  68. static extern bool ShowWindow( IntPtr hWnd, int nCmdShow );
  69. '@
  70.  
  71. $hideconsole = Add-Type -MemberDefinition $signature1 -Name Hide -Namespace HideConsole -ReferencedAssemblies System.Runtime.InteropServices -PassThru
  72.  
  73. # Hide console
  74. $hideconsole::ShowConsoleWindow( 0 )
  75.  
  76.  
  77. ##################
  78. # Disk inventory #
  79. ##################
  80.  
  81. [System.Collections.SortedList]$volumedetails = New-Object System.Collections.SortedList
  82. [System.Collections.SortedList]$volumestatus  = New-Object System.Collections.SortedList
  83.  
  84. $diskdrives = Get-WmiObject -Namespace "root/CIMV2" -Class Win32_DiskDrive
  85. $warnings   = $false
  86. foreach ( $disk in $diskdrives ) {
  87. 	$diskindex  = $disk.Index
  88. 	$diskmodel  = $disk.Model
  89. 	$disksize   = "{0,5:F0} GB" -f ( $disk.Size / 1GB )
  90. 	$diskstatus = $disk.Status
  91. 	$part_query = 'ASSOCIATORS OF {Win32_DiskDrive.DeviceID="' + $disk.DeviceID.replace('\','\\') + '"} WHERE AssocClass=Win32_DiskDriveToDiskPartition'
  92. 	$partitions = @( Get-WmiObject -Query $part_query | Sort-Object StartingOffset )
  93. 	foreach ( $partition in $partitions ) {
  94. 		$vol_query = 'ASSOCIATORS OF {Win32_DiskPartition.DeviceID="' + $partition.DeviceID + '"} WHERE AssocClass=Win32_LogicalDiskToPartition'
  95. 		$volumes   = @( Get-WmiObject -Query $vol_query )
  96. 		foreach ( $volume in $volumes ) {
  97. 			# 0 = Unknown; 1 = No Root Directory; 2 = Removable Disk; 3 = Local Disk; 4 = Network Drive; 5 = Compact Disc; 6 = RAM Disk
  98. 			# DriveType 3 means harddisks only
  99. 			if ( $volume.DriveType -eq 3 ) {
  100. 				if ( -not $volumedetails.Contains( $volume.Name ) ) {
  101. 					$volumedetails.Add( $volume.Name, "[Disk {0,2}]  {1}  {2}" -f ( $diskindex, $disksize, $diskmodel ) )
  102. 					$volumestatus.Add( $volume.Name, $diskstatus )
  103. 				}
  104. 			}
  105. 		}
  106. 	}
  107. }
  108.  
  109.  
  110. #################
  111. # Dialog window #
  112. #################
  113.  
  114. Add-Type -AssemblyName System.Windows.Forms
  115.  
  116. [System.Windows.Forms.Application]::EnableVisualStyles( )
  117.  
  118. $form = New-Object System.Windows.Forms.Form
  119. $form.Width           = 600
  120. $form.Height          = 25 * $volumedetails.Count + 120
  121. $form.Font            = 'Courier New, 10'
  122. $form.BackColor       = 'White'
  123. $form.MaximizeBox     = $false;
  124. $form.FormBorderStyle = 'FixedSingle'
  125.  
  126. $y    = 0
  127. $even = $false
  128. $volumedetails.Keys | ForEach-Object {
  129. 	$label           = New-Object System.Windows.Forms.Label
  130. 	$label.Size      = '35, 20'
  131. 	$label.Location  = New-Object System.Drawing.Point( 10, ( 15 + $y ) )
  132. 	$label.Text      = "$_"
  133. 	if ( $even ) { $label.BackColor = 'ButtonFace' }
  134. 	$form.Controls.Add( $label )
  135.  
  136. 	$status          = ( $volumestatus[$_] )
  137. 	$label           = New-Object System.Windows.Forms.Label
  138. 	$label.Size      = '40, 20 '
  139. 	$label.Location  = New-Object System.Drawing.Point( 45, ( 15 + $y ) )
  140. 	$label.Text      = $status
  141. 	if ( $status -eq "OK" ) {
  142. 		$label.ForeColor = 'Green'
  143. 	} else {
  144. 		$label.ForeColor = 'Red'
  145. 		$warnings        = $true
  146. 	}
  147. 	if ( $even ) { $label.BackColor = 'ButtonFace' }
  148. 	$form.Controls.Add( $label )
  149.  
  150. 	$label           = New-Object System.Windows.Forms.Label
  151. 	$label.Size      = '490, 20'
  152. 	$label.Location  = New-Object System.Drawing.Point( 85, ( 15 + $y ) )
  153. 	$label.Text      = $volumedetails[$_]
  154. 	if ( $even ) { $label.BackColor = 'ButtonFace' }
  155. 	$form.Controls.Add( $label )
  156.  
  157. 	$y    = $y + 25
  158. 	$even = -not $even
  159. }
  160.  
  161. if ( $warnings ) {
  162. 	$form.Text = "HDD Status Warning"
  163. } else {
  164. 	$form.Text = "HDD Status OK"
  165. }
  166.  
  167. $buttonOK           = New-Object System.Windows.Forms.Button
  168. $buttonOK.BackColor = 'ButtonFace'
  169. $buttonOK.Text      = "OK"
  170. $buttonOK.Size      = '60,24'
  171. $buttonOK.Location  = New-Object System.Drawing.Point( 85, ( 30 + $y ) )
  172. $form.Controls.Add( $buttonOK )
  173. $form.AcceptButton  = $buttonOK # Pressing Enter  closes the dialog
  174. $form.CancelButton  = $buttonOK # Pressing Escape closes the dialog
  175.  
  176.  
  177. ########################################
  178. # System tray balloon tip notification #
  179. ########################################
  180.  
  181. [System.Windows.Forms.ToolTipIcon]$icon = [System.Windows.Forms.ToolTipIcon]::Info
  182. $title = "HDD Status OK"
  183. $systraymessage = ""
  184. $volumedetails.Keys | ForEach-Object {
  185. 	$status = ( $volumestatus[$_] )
  186. 	if ( $status -ne "OK" ) {
  187. 		$icon = [System.Windows.Forms.ToolTipIcon]::Error
  188. 		$title = "Warning: HDD Errors"
  189. 	}
  190. 	$systraymessage = $systraymessage + "$_`t$status`n"
  191. }
  192.  
  193.  
  194. ################################################################
  195. # Extract system tray icon from Shell32.dll                    #
  196. # C# code to extract icons from Shell32.dll by Thomas Levesque #
  197. # http://stackoverflow.com/questions/6873026                   #
  198. ################################################################
  199.  
  200. $signature2 = @'
  201. [DllImport( "Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall )]
  202. private static extern int ExtractIconEx( string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons );
  203.  
  204. public static Icon Extract( string file, int number, bool largeIcon )
  205. {
  206. 	IntPtr large;
  207. 	IntPtr small;
  208. 	ExtractIconEx( file, number, out large, out small, 1 );
  209. 	try
  210. 	{
  211. 		return Icon.FromHandle( largeIcon ? large : small );
  212. 	}
  213. 	catch
  214. 	{
  215. 		return null;
  216. 	}
  217. }
  218. '@
  219.  
  220. $iconextractor = Add-Type -MemberDefinition $signature2 -Name IconExtract -Namespace IconExtractor -ReferencedAssemblies System.Windows.Forms,System.Drawing -UsingNamespace System.Windows.Forms,System.Drawing -PassThru
  221.  
  222. # System tray icon depends on status
  223. if( $title -eq "HDD Status OK" ) {
  224. 	$systrayicon = $iconextractor::Extract( "C:\Windows\System32\shell32.dll", 223, $true )
  225. } else {
  226. 	$systrayicon = $iconextractor::Extract( "C:\Windows\System32\shell32.dll", 53, $true )
  227. }
  228.  
  229. # Show system tray icon and balloon tip
  230. $notify = New-Object System.windows.Forms.NotifyIcon
  231. $notify.BalloonTipText = $systraymessage
  232. $notify.BalloonTipTitle = $title
  233. $notify.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]$icon
  234. $notify.Icon = $systrayicon
  235. $notify.Visible = $true
  236. $notify.ShowBalloonTip( 30000 )
  237.  
  238. # Show dialog
  239. [void] $form.ShowDialog( )
  240.  
  241.  
  242. ##########################################
  243. # Restore console minimized (2)          #
  244. # Change to 1 to restore to normal state #
  245. ##########################################
  246.  
  247. $hideconsole::ShowConsoleWindow( 2 )
  248.  
  249.  
  250. #################################
  251. # Exit code 1 in case of errors #
  252. #################################
  253.  
  254. if ( $warnings ) { exit 1 }
  255.  

page last uploaded: 2019-01-21, 22:48