In the previous trick, we have introduced, a sound progress bar. When PowerShell is doing a busy task, you can always let it play a piece of music. The code is as follows:
Copy Code code as follows:
# Locate the WAV audio file to be available under Windows folder
$WAVPath = Get-childitem-path $env: windir-filter *.wav-recurse-erroraction silentlycontinue |
Select-object-first 1-expandproperty FullName
# Load and Play
$player = New-object Media.soundplayer $WAVPath
$player. Playlooping ()
1..100 | Foreach-object {
Write-progress-activity ' doing something. Hang in '-status $_-percentcomplete $_
Start-sleep-milliseconds (get-random-minimum 300-maximum 1300)
}
$player. Stop ()
The script would have worked, but when you terminated it, like using CTRL + +, the script ended immediately. The last line of $player. Stop () Too late to execute, prompting the sound is still in buzzing, 3rd.
Solution, will be the last sentence to end the work of the $player. Stop () is placed in the finally statement:
Copy Code code as follows:
# Find WAV audio in Windows folder ask
$WAVPath = Get-childitem-path $env: Windir-filter *.wav-recurse-erroractionsilentlycontinue |
Select-object-first 1-expandproperty FullName
# Load and Play
$player = New-object Media.soundplayer $WAVPath
Try
{
$player. Playlooping ()
1..100 | Foreach-object {
Write-progress-activity ' doing something '-status $_-percentcomplete $_
Start-sleep-milliseconds (get-random-minimum 300-maximum 1300)
}
}
Finally
{
$player. Stop ()
}