WMI为quering主动网络和准连接主动、网络、WMI、quering

2023-09-06 05:45:45 作者:冇伱、已哫夠

我需要更改网络设置,如在this文章。这工作好为止。不过,我还需要知道什么活动的网络完成的更改。

I need to change Network settings like described in this article. That works good so far. However I also need to know on what active network I make the changes.

(为了更好的理解,请打开控制面板\网络和Internet \网络和共享中心。不幸的是所有的图片托管网站被封锁我的公司,所以我不能张贴的截图。)

(For a better understanding please open Control Panel\Network and Internet\ Network and Sharing Center. Unfortunately all picture hosting sites are blocked by my company so I can't post a screenshot.)

我怎么能查询哪些连接,与什么样的网络使用WMI(或其他技术)?

Any help on how I can query what connection is associated with what network with WMI (or other technology)?

更新: 我需要查询远程计算机。

UPDATE: I need to query a remote machine.

推荐答案

您可以使用 NetworkListManager COM组件,无论是与动态的,如下图所示,或者使用的 Windows API的code包其中包含了所有的COM包装。

You can use the NetworkListManager COM component, either with dynamic as shown below or using the Windows API Code Pack which contains all the COM wrappers.

dynamic networkListManager = Activator.CreateInstance(
     Type.GetTypeFromCLSID(new Guid("{DCB00C01-570F-4A9B-8D69-199FDBA5723B}")));

var connections = networkListManager.GetNetworkConnections();
foreach (var connection in connections)
{
    var network = connection.GetNetwork();
    Console.WriteLine("Network Name: " + network.GetName());
    Console.WriteLine("Network Category " + 
        network.GetCategory()+ " (0 public / 1 private / 2 Authenticated AD)" );

}

PowerShell的:

PowerShell:

$networkType = [Type]::GetTypeFromCLSID('DCB00C01-570F-4A9B-8D69-199FDBA5723B')
$networkListManager = [Activator]::CreateInstance($networkType)

$netWorks = $networkListManager.GetNetworkConnections()

foreach ($network in $netWorks)
{
    $name = $network.GetName()
    $category = $network.GetCategory()

    write-host "Network Name: $name"
    write-host "Network Category: $category"
}