需要使用Powershell根据数组中的名称选择一个项目 [英] need to select an item based on the name in an array using powershell

查看:72
本文介绍了需要使用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"?

推荐答案

显然,我在-Name参数" rel ="nofollow noreferrer" > 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆