PowerShell wait music

My long running PowerShell scripts now have background musak thanks to:


$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$musakFilePath="$scriptDir\musak.mp3"
$wmplayer = New-Object System.Windows.Media.MediaPlayer
$wmplayer.Open($musakFilePath)
Start-Sleep 2 # This allows the $wmplayer time to load the audio file
$duration = $wmplayer.NaturalDuration.TimeSpan.TotalSeconds
$wmplayer.Play()
$stopwatch=[system.diagnostics.stopwatch]::StartNew()
while ($stopwatch.Elapsed.Seconds -lt $duration)
{
Write-Progress -Activity “Doing stuff, please hold…” -status “$($stopwatch.Elapsed.Seconds) seconds” -percentComplete ($stopwatch.Elapsed.Seconds / $duration*100)
# do something
# break when done
start-Sleep -s 1
}
$wmplayer.Stop()
$wmplayer.Close()

Leave a Reply