需要使用Powershell根据数组中的名称选择一个项目 [英] need to select an item based on the name in an array using powershell
问题描述
甚至不确定我是不是说的正确,但是这里什么也没做:我有一个脚本可在音频设备之间切换.它将根据索引号检查当前设置的设备,并相应地切换到另一台设备.我的问题是,如果我引入新设备,索引号会更改.所以我需要根据名称而不是索引选择设备,我该怎么做?
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
}
Get-AudioDevicelist
的输出如下:
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
如果我关闭电视,索引号会更改,然后脚本停止工作. .Index将排在第一列,我如何使它看起来像"Logitech" 或扬声器"?
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"?
推荐答案
显然,我在set-defaultaudiodevice
,只是将$audio.Index
更改为.devicefriendlyname
并相应地更新了if
语句
Apparently there's a -Name
parameter I missed on set-defaultaudiodevice
, just changed $audio.Index
to .devicefriendlyname
and updated the if
statement accordingly
这篇关于需要使用Powershell根据数组中的名称选择一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!