Skip to content

Instantly share code, notes, and snippets.

@sean-m
Created May 8, 2023 15:08
Show Gist options
  • Save sean-m/e0061fc83d1813aebffbcd516a70526c to your computer and use it in GitHub Desktop.
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.
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