WMI for quering active networks and associate connections
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.
(For a better understanding please open Co开发者_运维问答ntrol 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.)
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.
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:
$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"
}
精彩评论