From fb846edd5a7bd6c9371fee29638c5a652c753de7 Mon Sep 17 00:00:00 2001 From: Tomohide Fujikawa Date: Mon, 1 May 2023 07:12:29 +0900 Subject: [PATCH] Divide example code and add a .bat file to boot .ps1 file --- EXAMPLE/BootSwitchAudioDevice.bat | 2 ++ EXAMPLE/PlaySound.ps1 | 27 +++++++++++++++++++ ...-AudioDevice.ps1 => SwitchAudioDevice.ps1} | 8 +----- 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 EXAMPLE/BootSwitchAudioDevice.bat create mode 100644 EXAMPLE/PlaySound.ps1 rename EXAMPLE/{Toggle-AudioDevice.ps1 => SwitchAudioDevice.ps1} (82%) diff --git a/EXAMPLE/BootSwitchAudioDevice.bat b/EXAMPLE/BootSwitchAudioDevice.bat new file mode 100644 index 0000000..f328ea2 --- /dev/null +++ b/EXAMPLE/BootSwitchAudioDevice.bat @@ -0,0 +1,2 @@ +start powershell -file .\SwitchAudioDevice.ps1 +exit diff --git a/EXAMPLE/PlaySound.ps1 b/EXAMPLE/PlaySound.ps1 new file mode 100644 index 0000000..0ca149e --- /dev/null +++ b/EXAMPLE/PlaySound.ps1 @@ -0,0 +1,27 @@ +<# +Copyright (c) 2016-2022 Francois Gendron +MIT License + +This file is a script that toggles between two playback audio devices using +AudioDeviceCmdlets then plays a sound for confirmation +AudioDeviceCmdlets is a suite of PowerShell Cmdlets to control audio devices +on Windows +https://github.com/frgnca/AudioDeviceCmdlets +#> + +# This script toggles between two playback audio devices then plays a sound for confirmation +# The devices are defined by their ID (Get-AudioDevice -List) + +# Bonus: Run this PowerShell script from a VBScript to avoid visible window +# Toggle-AudioDevice.vbs +<# +command = "powershell.exe -nologo -command C:\Path\To\PlaySound.ps1" +set shell = CreateObject("WScript.Shell") +shell.Run command,0 +#> + +# Play sound +$Sound = new-Object System.Media.SoundPlayer +$Sound.SoundLocation = "c:\WINDOWS\Media\Windows Background.wav" +$Sound.Play() +Start-Sleep -s 3 diff --git a/EXAMPLE/Toggle-AudioDevice.ps1 b/EXAMPLE/SwitchAudioDevice.ps1 similarity index 82% rename from EXAMPLE/Toggle-AudioDevice.ps1 rename to EXAMPLE/SwitchAudioDevice.ps1 index 7aa2812..e38bd36 100644 --- a/EXAMPLE/Toggle-AudioDevice.ps1 +++ b/EXAMPLE/SwitchAudioDevice.ps1 @@ -15,7 +15,7 @@ https://github.com/frgnca/AudioDeviceCmdlets # Bonus: Run this PowerShell script from a VBScript to avoid visible window # Toggle-AudioDevice.vbs <# -command = "powershell.exe -nologo -command C:\Path\To\Toggle-AudioDevice.ps1" +command = "powershell.exe -nologo -command C:\Path\To\SwitchAudioDevice.ps1" set shell = CreateObject("WScript.Shell") shell.Run command,0 #> @@ -28,9 +28,3 @@ $AudioDevice_B = "{0.0.0.00000000}.{c4aadd95-74c7-4b3b-9508-b0ef36ff71ba}" $DefaultPlayback = Get-AudioDevice -Playback If ($DefaultPlayback.ID -eq $AudioDevice_A) {Set-AudioDevice -ID $AudioDevice_B | Out-Null} Else {Set-AudioDevice -ID $AudioDevice_A | Out-Null} - -# Play sound -$Sound = new-Object System.Media.SoundPlayer -$Sound.SoundLocation = "c:\WINDOWS\Media\Windows Background.wav" -$Sound.Play() -Start-Sleep -s 3