如何在Powershell中识别默认音频设备?

如何在Powershell中识别默认音频设备?,powershell,audio-device,Powershell,Audio Device,我正在寻找通过Powershell获取默认音频设备的解决方案。 在最好的情况下,它可以通过嵌入式C#代码直接使用IMMDeviceEnumerator::GetDefaultAudioEndpoint(请参见此处) 但是如果通过RegKeys更容易获得,那么这也是可以的。 我看到一些代码片段从HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render或\Capture读取密钥,但我仍然难以识别默认设备 似乎,当我修改

我正在寻找通过Powershell获取默认音频设备的解决方案。 在最好的情况下,它可以通过嵌入式C#代码直接使用IMMDeviceEnumerator::GetDefaultAudioEndpoint(请参见此处)

但是如果通过RegKeys更容易获得,那么这也是可以的。 我看到一些代码片段从HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio\Render或\Capture读取密钥,但我仍然难以识别默认设备

似乎,当我修改设备的顺序时,我可以简单地搜索活动设备(DeviceState=1),然后按值“级别:0”、“级别:1”和“级别:2”排序,但级别值在用户未手动修改顺序的系统上不可用。在这种情况下,排序标准是什么

这是通过RegKeys解决此问题的代码片段,但正如所述-并非适用于所有情况:

$regAudio =  "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\MMDevices\Audio"
$nameId = "{b3f8fa53-0004-438e-9003-51a46e139bfc},6"
$classId = "{a45c254e-df1c-4efd-8020-67d146a850e0},2"
$driverDetails = "{83da6326-97a6-4088-9453-a1923f573b29},3"

function get-DefaultDevice($type) {
    $activeDevices = foreach($key in  Get-ChildItem "$regAudio\$type\") {
        foreach($item in Get-ItemProperty $key.PsPath) { 
            if ($item.DeviceState -eq $activeState) {$item}
        }
    }
    $defaultDevice = $activeDevices | Sort-Object -Property "Level:0","Level:1","Level:2" | select -last 1
    $details = Get-ItemProperty "$($defaultDevice.PSPath)\Properties"
    $name = "$($details.$classId) ($($details.$nameId))"
    return @{
        name   = $name
        driver = $details.$driverDetails
    }
}

$OsRender = get-DefaultDevice "Render"
$OsCapture = get-DefaultDevice "Capture"

有没有办法“以智能的方式”(当然没有任何外部DLL)获取此信息?

最后我找到了答案,我很高兴与大家分享工作代码片段:

cls
Add-Type -TypeDefinition @'
using System.Runtime.InteropServices;
[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDevice {
    int a(); int o();
    int GetId([MarshalAs(UnmanagedType.LPWStr)] out string id);
}
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDeviceEnumerator {
    int f();
    int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint);
}
[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumeratorComObject { }
public class Audio {
    public static string GetDefault (int direction) {
    var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
    IMMDevice dev = null;
    Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(direction, 1, out dev));
    string id = null;
    Marshal.ThrowExceptionForHR(dev.GetId(out id));
    return id;
    }
}
'@

function getFriendlyName($id) {
    $reg = "HKLM:\SYSTEM\CurrentControlSet\Enum\SWD\MMDEVAPI\$id"
    return (get-ItemProperty $reg).FriendlyName
}

$id0 = [audio]::GetDefault(0)
$id1 = [audio]::GetDefault(1)
write-host "Default Speaker: $(getFriendlyName $id0)" 
write-host "Default Micro  : $(getFriendlyName $id1)"

超过1000个视图的解决方案,并没有投票支持它-我不明白这一点。您好,先生,实际上这段代码是非常好的,当我能够正常运行它。有时它会给我一个错误,不知道为什么。。。无论如何,这是一部精彩的作品!!!:-)非常感谢。