问题
Not even sure if i'm wording this right but here goes nothing: I have a script to toggle through audio devices. It will check the current set device based on Index number, and switch to the other one accordingly. My problem is, if i introduce a new device, the index numbers change. So i need to select the device based on name not index, how do I do that?
$audio = Get-DefaultAudioDevice
$Audio.Index
if ($audio.Index -eq '0') {
set-defaultaudiodevice 2
} Else {
set-defaultaudiodevice 0
}
the output for Get-AudioDevicelist
is as follows:
Index DeviceFriendlyname Device
----- ------------------ ------
0 Speakers (Realtek High Definition Audio) CoreAudioApi.MMDevice
1 LG TV-0 (NVIDIA High Definition Audio) CoreAudioApi.MMDevice
2 Speakers (Logitech G533 Gaming Headset) CoreAudioApi.MMDevice
If i turn off the TV, the index number changes and then the script stops working. .Index will go by the first column, how do i make it look for "Logitech" or "Speakers"?
回答1:
Apparently there's a -Name
parameter I missed on set-defaultaudiodevice, just changed $audio.Index
to .devicefriendlyname
and updated the if
statement accordingly
来源:https://stackoverflow.com/questions/47361611/need-to-select-an-item-based-on-the-name-in-an-array-using-powershell