
デフォルトの通信デバイス PowerShell。
PowerShell でオーディオ デバイス出力を既定の通信デバイスとして設定することは可能ですか? スピーカーとヘッドフォンを切り替えるため、この機能が必要です。PowerShell で AudioDeviceCmdlets を使用して、オーディオ デバイスを既定の出力として設定しています。
すでにレジストリで試してみましたが、PowerShell ではその特定の部分の値を変更できません (PowerShell を管理者モードで実行しました)
これを使って、どのデバイスがオフまたはオンになっているかを確認し、出力としてスピーカーまたはヘッドフォンに切り替えるスクリプトを作成したいと思います。
このコードは既に持っています:
if((Get-PnpDevice -InstanceId 'INSTANCEID' | Select-Object -Property status) -eq "OK"){ Disable-PnpDevice -InstanceId 'INSTANCEID' -confirm:$false Enable-PnpDevice -InstanceId 'INSTANCEID' -confirm:$false } Else { Enable-PnpDevice -InstanceId 'INSTANCEID' -confirm:$false }
デフォルトのオーディオデバイス出力ヘッドフォン GAME Set-AudioDevice -ID "deviceid"
デフォルトのオーディオデバイス出力スピーカー Set-AudioDevice -ID "deviceid"
戦車。
答え1
PowerShell でこれを試したことはないのですが、PowerShell からこれを使用しています:
nircmd.exe setdefaultsounddevice FrontHeadphones
FrontHeadphones
オーディオデバイスの名前はどこにありますか?NirSoft の nircmdこのために。
答え2
以下を試して、必要に応じて調整してください。
コントロール パネル ダイアログが内部で行っているのは、いくつかのレジストリ設定の変更だけです。私が欲しかったのは、あるプロファイルから別のプロファイルに変更できるスクリプトです。
$r = [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$regRoot = "HKCU:\Software\Microsoft\"
$profiles = @{"Netbook" = @("Realtek HD Audio output",
"Realtek HD Audio input");
"Bluetooth" = @("Bluetooth Hands-free Audio",
"Bluetooth Hands-free Audio") }
function Write-Message ( [string]$message )
{
echo $message
# Uncomment this line to show dialog outputs from -set
# $r = [System.Windows.Forms.MessageBox]::Show($message)
}
function Set-Mapping ( [string]$devOut, [string]$devIn )
{
echo "Profile audio:`n in = $devIn`n out = $devOut"
$regKey = $regRoot + "\Multimedia\Sound Mapper\"
Set-ItemProperty $regKey -name Playback -value $devOut
Set-ItemProperty $regKey -name Record -value $devIn
}
function List-Devices
{
$regKey = $regRoot + "\Windows\CurrentVersion\Applets\Volume Control\"
echo "Sound devices:"
ls $regKey | where { ! $_.Name.EndsWith("Options") } |
Foreach-Object {
echo (" " + $_.Name.Substring($_.Name.LastIndexOf("\")+1))
}
}
$cmd = $args[0]
switch ($cmd)
{
"-profiles"
{
echo "Sound profiles:"
echo $profiles
}
"-devices"
{
List-Devices
}
"-set"
{
$p = $args[1]
if (!$profiles.ContainsKey($p)) {
echo "No such profile: $p"
echo $profiles
exit
}
Set-Mapping $profiles.Get_Item($p)[0] $profiles.Get_Item($p)[1]
Write-Message "Profile set to: $p"
}
default
{
Write-Message "No such option: $cmd"
}
}
ご覧のとおり、3 つのオプションがあります。
-profiles – 使用可能なサウンド プロファイルを一覧表示します。
-devices – サウンド デバイスを一覧表示します。
-set [profile] – オーディオ プロファイルを設定します。