Created
May 8, 2023 15:08
-
-
Save sean-m/e0061fc83d1813aebffbcd516a70526c to your computer and use it in GitHub Desktop.
Take pipeline input and beep when the input changes. It will optionally stop when the change occurs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function BeepOnChange { | |
<# | |
.Synopsis | |
Take pipeline input and beep when the input changes. | |
It will optionally stop when the change occurs. | |
.PARAMETER Skip | |
Number of lines to not consider for comparison. If you're piping | |
a long running command like ping /t, you want it to skip the | |
header of the command. Combine ping with the StopOnChange switch | |
for it to alert when a network connection comes back up and ping | |
no longer reports "Request timed out." | |
.EXAMPLE | |
& ping 192.168.1.1 | BeepOnChange -Skip 2 -StopOnChange | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter(ValueFromPipeline=$true,Position=0)] | |
$InputObject, | |
[int]$Skip=0, | |
[switch]$StopOnChange | |
) | |
begin { | |
$line = "" | |
$iter = 0 | |
$changed = $false | |
} | |
process { | |
$InputObject | foreach { | |
if ($iter -ge $Skip) { | |
if ($line -notlike $_ -and $iter -ne 0) { | |
$changed = $true | |
} | |
} | |
$line = $_ | |
$iter++ | |
$_ | |
if ($changed) { | |
[Console]::Beep() | |
if ($StopOnChange) { | |
continue | |
} | |
$changed = $false | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment