(view source code of monitorping.ps as plain text)
<#
.SYNOPSIS
Monitor the availability of a computer, printer or any networked device on the network
.DESCRIPTION
This script checks the network connection to the specified device, at the specified interval.
It uses WMI's Win32_PingStatus to check the connection.
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.
With the -Beep parameter a change of connection status will produce a short three tone beep.
Note: When running hidden, you won't be able to stop the monitoring except by terminating the (right) powershell process!
.PARAMETER Device
The host name or IP address of the device to monitor
.PARAMETER Interval
The interval between checks in seconds
PARAMETER Beep
Beep whenever the connection status changes
PARAMETER Hide
Hide the console
.EXAMPLE
MonitorPing.ps1 MYOTHERPC -Interval 30 -Beep -Hide
Will check the network connection to MYOTHERPC every 30 seconds.
The result is shown in the Notification Area.
On status changes, a short beep will be produced.
.LINK
Script written by Rob van der Woude
https://www.robvanderwoude.com/
.LINK
Code to hide the console window by Anthony on StackOverflow.com:
http://stackoverflow.com/a/15079092
.LINK
C# code to extract icons from Shell32.dll by Thomas Levesque on StackOverflow.com:
http://stackoverflow.com/questions/6873026
#>
param(
[string]$Device = $( Clear-Host ; Read-Host "`n`n Please enter the name or IP address of the device to monitor" ),
[int]$Interval = 10,
[switch]$Beep,
[switch]$Hide
)
Clear-Host
if ( [string]::IsNullOrWhiteSpace( $Device ) -or ( $Device -match "[\\/\?\*:;,]" ) ) {
Get-Help ( Join-Path $PSScriptRoot 'MonitorPing.ps1' ) -Full
exit -1
}
#######################################
# Hide console window #
# by Anthony on StackOverflow.com #
# http://stackoverflow.com/a/15079092 #
#######################################
$signature1 = @'
public static void ShowConsoleWindow( int state )
{
var handle = GetConsoleWindow( );
ShowWindow( handle, state );
}
[System.Runtime.InteropServices.DllImport( "kernel32.dll" )]
static extern IntPtr GetConsoleWindow( );
[System.Runtime.InteropServices.DllImport( "user32.dll" )]
static extern bool ShowWindow( IntPtr hWnd, int nCmdShow );
'@
$hideconsole = Add-Type -MemberDefinition $signature1 -Name Hide -Namespace HideConsole -ReferencedAssemblies System.Runtime.InteropServices -PassThru
# Hide console
if ( $Hide ) {
$hideconsole::ShowConsoleWindow( 0 )
}
################################################################
# Extract system tray icon from Shell32.dll #
# C# code to extract icons from Shell32.dll by Thomas Levesque #
# http://stackoverflow.com/questions/6873026 #
################################################################
$signature2 = @'
[DllImport( "Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true, CallingConvention = CallingConvention.StdCall )]
private static extern int ExtractIconEx( string sFile, int iIndex, out IntPtr piLargeVersion, out IntPtr piSmallVersion, int amountIcons );
public static Icon Extract( string file, int number, bool largeIcon )
{
IntPtr large;
IntPtr small;
ExtractIconEx( file, number, out large, out small, 1 );
try
{
return Icon.FromHandle( largeIcon ? large : small );
}
catch
{
return null;
}
}
'@
$iconextractor = Add-Type -MemberDefinition $signature2 -Name IconExtract -Namespace IconExtractor -ReferencedAssemblies System.Windows.Forms,System.Drawing -UsingNamespace System.Windows.Forms,System.Drawing -PassThru
# Show system tray icon and balloon tip
$notify = New-Object System.windows.Forms.NotifyIcon
$notify.BalloonTipText = "Checking status of printer 'Blacky'"
$notify.BalloonTipTitle = "Printer status 'Blacky'"
$notify.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Info
$notify.Icon = $iconextractor::Extract( "C:\Windows\System32\shell32.dll", 226, $true ) # 23 210 226 268
$notify.Visible = $true
$notify.ShowBalloonTip( 30000 )
$status = -1
$query = "Select * From Win32_PingStatus Where Address='{0}'" -f $Device
while ( $true ) {
$ping = ( Get-WmiObject -Query $query )
if ( $status -ne $ping.StatusCode ) {
if ( $ping.StatusCode -eq 0 ) {
$notify.BalloonTipText = ( "{0} is ONline" -f $Device )
$notify.Icon = $iconextractor::Extract( "C:\Windows\System32\shell32.dll", 82, $true ) # 82 197 222
$notify.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Info
if ( -not $Hide ) {
Clear-Host
Write-Host ( "`n`n{0} is " -f $Device ) -NoNewline
Write-Host "online" -ForegroundColor Green
Write-Host "`n`n`n`nPress Ctrl+Break to stop monitoring"
}
} else {
$notify.BalloonTipText = ( "{0} is OFFline" -f $Device )
$notify.Icon = $iconextractor::Extract( "C:\Windows\System32\shell32.dll", 131, $true ) # 131 228 229
$notify.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
if ( -not $Hide ) {
Clear-Host
Write-Host ( "`n`n{0} is " -f $Device ) -NoNewline
Write-Host "offline" -ForegroundColor Red
Write-Host "`n`n`n`nPress Ctrl+Break to stop monitoring"
}
}
if ( $Beep ) {
[System.Console]::Beep( 900, 250 )
[System.Console]::Beep( 1800, 125 )
[System.Console]::Beep( 3600, 125 )
}
}
$status = $ping.StatusCode
Start-Sleep -Seconds $interval
}
page last modified: 2024-04-16; loaded in 0.0072 seconds