Network List Manager in PowerShell

I’m writing a network diagnostic script, and wanted to use the network status and connectivity information that’s already been captured by the Network List Manager (NLM).

The code for pulling various bits of info is below. NLM_Connectivity was a good opportunity to learn how to work with enumeration of flags, so I’ve included several examples of working with these, to both list all the active connectivity types, and also ways to check for a specific connectivity type.

$NetworkListManager = [Activator]::CreateInstance([Type]::GetTypeFromCLSID(‘DCB00C01-570F-4A9B-8D69-199FDBA5723B’))

# Set enums for GetNetworks
$NLM_ENUM_NETWORK_CONNECTED=1
$NLM_ENUM_NETWORK_DISCONNECTED=2
$NLM_ENUM_NETWORK_ALL=3

$Networks = $NetworkListManager.GetNetworks($NLM_ENUM_NETWORK_CONNECTED)

foreach($Network in $Networks){
# Network name
$Network.GetName()

# Values from INetworkListManager interface https://msdn.microsoft.com/en-us/library/windows/desktop/aa370769(v=vs.85).aspx

# Network category
$NetCategories = New-Object -TypeName System.Collections.Hashtable
$NetCategories.Add(0x00,"NLM_NETWORK_CATEGORY_PUBLIC")
$NetCategories.Add(0x01,"NLM_NETWORK_CATEGORY_PRIVATE")
$NetCategories.Add(0x02,"NLM_NETWORK_CATEGORY_DOMAIN_AUTHENTICATED")
$NetCategories.Get_Item($Network.GetCategory())

# Domain type
$DomainTypes = New-Object -TypeName System.Collections.Hashtable
$DomainTypes.Add(0x00,"NLM_DOMAIN_TYPE_NON_DOMAIN_NETWORK")
$DomainTypes.Add(0x01,"NLM_DOMAIN_TYPE_DOMAIN_NETWORK")
$DomainTypes.Add(0x02,"NLM_DOMAIN_TYPE_DOMAIN_AUTHENTICATED")
$DomainTypes.Get_Item($Network.GetDomainType())

# NLM Connectivity
$NLMConnectivity = New-Object -TypeName System.Collections.Hashtable
$NLMConnectivity.Add(0x0000,"NLM_CONNECTIVITY_DISCONNECTED")
$NLMConnectivity.Add(0x0001,"NLM_CONNECTIVITY_IPV4_NOTRAFFIC")
$NLMConnectivity.Add(0x0002,"NLM_CONNECTIVITY_IPV6_NOTRAFFIC")
$NLMConnectivity.Add(0x0010,"NLM_CONNECTIVITY_IPV4_SUBNET")
$NLMConnectivity.Add(0x0020,"NLM_CONNECTIVITY_IPV4_LOCALNETWORK")
$NLMConnectivity.Add(0x0040,"NLM_CONNECTIVITY_IPV4_INTERNET")
$NLMConnectivity.Add(0x0100,"NLM_CONNECTIVITY_IPV6_SUBNET")
$NLMConnectivity.Add(0x0200,"NLM_CONNECTIVITY_IPV6_LOCALNETWORK")
$NLMConnectivity.Add(0x0400,"NLM_CONNECTIVITY_IPV6_INTERNET")

# Several methods for working with the connectivity flags

# Display all active connectivity types (method a)
foreach($Key in $NLMConnectivity.Keys){
$KeyBand = $Key -band $net.GetConnectivity()
if($KeyBand -gt 0){
$NLMConnectivity.Get_Item($KeyBand)
}
}

# Display all active connectivity types (method b)
$NLMConnectivity.Keys | Where-Object {$_ -band $Network.GetConnectivity()} | ForEach-Object {$NLMConnectivity.Get_Item($_)}

# Display all active connectivity types (method c)
switch ($Network.GetConnectivity()){
{$_ -band 0x0000}{"NLM_CONNECTIVITY_DISCONNECTED"}
{$_ -band 0x0001}{"NLM_CONNECTIVITY_IPV4_NOTRAFFIC"}
{$_ -band 0x0002}{"NLM_CONNECTIVITY_IPV6_NOTRAFFIC"}
{$_ -band 0x0010}{"NLM_CONNECTIVITY_IPV4_SUBNET"}
{$_ -band 0x0020}{"NLM_CONNECTIVITY_IPV4_LOCALNETWORK"}
{$_ -band 0x0040}{"NLM_CONNECTIVITY_IPV4_INTERNET"}
{$_ -band 0x0100}{"NLM_CONNECTIVITY_IPV6_SUBNET"}
{$_ -band 0x0200}{"NLM_CONNECTIVITY_IPV6_LOCALNETWORK"}
{$_ -band 0x0400}{"NLM_CONNECTIVITY_IPV6_INTERNET"}
}

# Display all active connectivity types (method d)
Enum NLM_CONNECTIVITY {
NLM_CONNECTIVITY_DISCONNECTED = 0x0000
NLM_CONNECTIVITY_IPV4_NOTRAFFIC = 0x0001
NLM_CONNECTIVITY_IPV6_NOTRAFFIC = 0x0002
NLM_CONNECTIVITY_IPV4_SUBNET = 0x0010
NLM_CONNECTIVITY_IPV4_LOCALNETWORK = 0x0020
NLM_CONNECTIVITY_IPV4_INTERNET = 0x0040
NLM_CONNECTIVITY_IPV6_SUBNET = 0x0100
NLM_CONNECTIVITY_IPV6_LOCALNETWORK = 0x0200
NLM_CONNECTIVITY_IPV6_INTERNET = 0x0400
}
[enum]::GetValues([NLM_CONNECTIVITY]) | Where-Object {$_.value__ -band $Network.GetConnectivity()}

# Check for a particular type of connectivity (method a)
if(0x0040 -band $Network.GetConnectivity()){Write-Host "NLM_CONNECTIVITY_IPV4_INTERNET is active"}

# Check for a particular type of connectivity (method b)
$NLMConnectivityActiveFlags = $NLMConnectivity.Keys | Where-Object {$_ -band $Network.GetConnectivity()} | ForEach-Object {$NLMConnectivity.Get_Item($_)}
if($NLMConnectivityActiveFlags.Contains("NLM_CONNECTIVITY_IPV4_SUBNET")){Write-Host "NLM_CONNECTIVITY_IPV4_SUBNET is active"}
}

Advertisements
This entry was posted in PowerShell and tagged , , , , , , , , , . Bookmark the permalink.

One Response to Network List Manager in PowerShell

  1. mosaic says:

    Thank you! In order to make this work in PowerShell v4.0, I had to
    Change the enum into a hashtable
    Replace $net.GetConnectivity() with $network.GetConnectivity()
    Replace $NLMConnectivityActiveFlags.Contains with $NLMConnectivityActiveFlags -Contains
    Do you know a way to list all network locations that show up in Windows Explorer (similar to NET USE for mapped drives)?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s