need to select an item based on the name in an array using powershell

烈酒焚心 提交于 2019-12-13 02:35:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!