Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for monitorping.ps

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

  1. <#
  2. .SYNOPSIS
  3. Monitor the availability of a computer, printer or any networked device on the network
  4.  
  5. .DESCRIPTION
  6. This script checks the network connection to the specified device, at the specified interval.
  7. It uses WMI's Win32_PingStatus to check the connection.
  8. The result will be shown in the console, unless this is hidden by the -Hide parameter, and by an icon in the Notification Area (a.k.a. System Tray), different icon for different status.
  9. With the -Beep parameter a change of connection status will produce a short three tone beep.
  10.  
  11. Note: When running hidden, you won't be able to stop the monitoring except by terminating the (right) powershell process!
  12.  
  13. .PARAMETER Device
  14. The host name or IP address of the device to monitor
  15.  
  16. .PARAMETER Interval
  17. The interval between checks in seconds
  18.  
  19. PARAMETER Beep
  20. Beep whenever the connection status changes
  21.  
  22. PARAMETER Hide
  23. Hide the console
  24.  
  25. .EXAMPLE
  26. MonitorPing.ps1 MYOTHERPC -Interval 30 -Beep -Hide
  27. Will check the network connection to MYOTHERPC every 30 seconds.
  28. The result is shown in the Notification Area.
  29. On status changes, a short beep will be produced.
  30.  
  31. .LINK
  32. Script written by Rob van der Woude
  33. https://www.robvanderwoude.com/
  34.  
  35. .LINK
  36. Code to hide the console window by Anthony on StackOverflow.com:
  37. http://stackoverflow.com/a/15079092
  38.  
  39. .LINK
  40. C# code to extract icons from Shell32.dll by Thomas Levesque on StackOverflow.com:
  41. http://stackoverflow.com/questions/6873026
  42. #>
  43.  
  44. param(
  45. 	[string]$Device = $( Clear-Host ; Read-Host "`n`n  Please enter the name or IP address of the device to monitor" ),
  46. 	[int]$Interval = 10,
  47. 	[switch]$Beep,
  48. 	[switch]$Hide
  49. )
  50.  
  51. Clear-Host
  52.  
  53. if ( [string]::IsNullOrWhiteSpace( $Device ) -or ( $Device -match "[\\/\?\*:;,]" ) ) {
  54. 	Get-Help ( Join-Path $PSScriptRoot 'MonitorPing.ps1' ) -Full
  55. 	exit -1
  56. }
  57.  
  58. #######################################
  59. # Hide console window                 #
  60. # by Anthony on StackOverflow.com     #
  61. # http://stackoverflow.com/a/15079092 #
  62. #######################################
  63.  
  64. $signature1 = @'
  65. public static void ShowConsoleWindow( int state )
  66. {
  67. 	var handle = GetConsoleWindow( );
  68. 	ShowWindow( handle, state );
  69. }
  70.  
  71. [System.Runtime.InteropServices.DllImport( "kernel32.dll" )]
  72. static extern IntPtr GetConsoleWindow( );
  73.  
  74. [System.Runtime.InteropServices.DllImport( "user32.dll" )]
  75. static extern bool ShowWindow( IntPtr hWnd, int nCmdShow );
  76. '@
  77.  
  78. $hideconsole = Add-Type -MemberDefinition $signature1 -Name Hide -Namespace HideConsole -ReferencedAssemblies System.Runtime.InteropServices -PassThru
  79.  
  80. # Hide console
  81. if ( $Hide ) {
  82. 	$hideconsole::ShowConsoleWindow( 0 )
  83. }
  84.  
  85. ################################################################
  86. # Extract system tray icon from Shell32.dll                    #
  87. # C# code to extract icons from Shell32.dll by Thomas Levesque #
  88. # http://stackoverflow.com/questions/6873026                   #
  89. ################################################################
  90.  
  91. $signature2 = @'
  92. [DllImport( "Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall )]
  93. private static extern int ExtractIconEx( string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons );
  94.  
  95. public static Icon Extract( string file, int number, bool largeIcon )
  96. {
  97. 	IntPtr large;
  98. 	IntPtr small;
  99. 	ExtractIconEx( file, number, out large, out small, 1 );
  100. 	try
  101. 	{
  102. 		return Icon.FromHandle( largeIcon ? large : small );
  103. 	}
  104. 	catch
  105. 	{
  106. 		return null;
  107. 	}
  108. }
  109. '@
  110.  
  111. $iconextractor = Add-Type -MemberDefinition $signature2 -Name IconExtract -Namespace IconExtractor -ReferencedAssemblies System.Windows.Forms,System.Drawing -UsingNamespace System.Windows.Forms,System.Drawing -PassThru
  112.  
  113. # Show system tray icon and balloon tip
  114. $notify = New-Object System.windows.Forms.NotifyIcon
  115. $notify.BalloonTipText = "Checking status of printer 'Blacky'"
  116. $notify.BalloonTipTitle = "Printer status 'Blacky'"
  117. $notify.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Info
  118. $notify.Icon = $iconextractor::Extract( "C:\Windows\System32\shell32.dll", 226, $true ) # 23 210 226 268
  119. $notify.Visible = $true
  120. $notify.ShowBalloonTip( 30000 )
  121.  
  122. $status = -1
  123. $query = "Select * From Win32_PingStatus Where Address='{0}'" -f $Device
  124. while ( $true ) {
  125. 	$ping = ( Get-WmiObject -Query $query )
  126. 	if ( $status -ne $ping.StatusCode ) {
  127. 		if ( $ping.StatusCode -eq 0 ) {
  128. 			$notify.BalloonTipText = ( "{0} is ONline" -f $Device )
  129. 			$notify.Icon = $iconextractor::Extract( "C:\Windows\System32\shell32.dll", 82, $true ) # 82 197 222
  130. 			$notify.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Info
  131. 			if ( -not $Hide ) {
  132. 				Clear-Host
  133. 				Write-Host ( "`n`n{0} is " -f $Device ) -NoNewline
  134. 				Write-Host "online" -ForegroundColor Green
  135. 				Write-Host "`n`n`n`nPress Ctrl+Break to stop monitoring"
  136. 			}
  137. 		} else {
  138. 			$notify.BalloonTipText = ( "{0} is OFFline" -f $Device )
  139. 			$notify.Icon = $iconextractor::Extract( "C:\Windows\System32\shell32.dll", 131, $true ) # 131 228 229
  140. 			$notify.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
  141. 			if ( -not $Hide ) {
  142. 				Clear-Host
  143. 				Write-Host ( "`n`n{0} is " -f $Device ) -NoNewline
  144. 				Write-Host "offline" -ForegroundColor Red
  145. 				Write-Host "`n`n`n`nPress Ctrl+Break to stop monitoring"
  146. 			}
  147. 		}
  148.         if ( $Beep ) {
  149.     		[System.Console]::Beep( 900, 250 )
  150. 	    	[System.Console]::Beep( 1800, 125 )
  151. 		    [System.Console]::Beep( 3600, 125 )
  152.         }
  153. 	}
  154. 	$status = $ping.StatusCode
  155. 	Start-Sleep -Seconds $interval
  156. }
  157.  

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