Finding Connected Adapters
One of the most fundamental pieces of troubleshooting or security checks to do is to find out which of the many network adapters on a computer are actually connected to a network.
PowerTip : Show 'up' physical adapters
Question: You want to see which physical network adapters on your Windows 8.1 computer using Windows PowerShell. How can you do this?
Answer: Use the -physical parameter with the Get-NetAdapter function and filter for a status of up. This technique appears here:
1
Get-NetAdapter -physical| where status -eq 'up'
Copied!

Using NetSh

It is pretty easy to use NetSh to retrieve information about the connection status of network adapters. To do so, I use the following command:
1
netsh interface ipv4 show interfaces
Copied!
One of the problems, from a management perspective, is that the command returns text. Therefore, if I need to parse the text to pull out specific information, such as the Interface Index number, or the Name of the adapter, then I am going to have to resort to writing a complicated regular expression pattern. If all I need to do is to obtain the information because I am writing to a log file as text, then the command works great, and is the lowest common denominator - I can use it all the way back to Windows 2000 days.
I can even run the netsh commands from within the Windows PowerShell console. This appears in the figure that follows.
image043.png

Using WMI

It is possible to use WMI and the Win32_NetworkAdapter WMI class to retrieve information about the connection status. The NetConnectionStatus property reports backed in a coded value that reports the status. These values are documented on MSDN for the Win32_NetworkAdapter class. Using the Get-WmiObject Windows PowerShell cmdlet, I can work with any operating system that installs Windows PowerShell. This includes Windows XP, Windows Server 2003 and above. The following command returns information similar to the NetSh command.
1
get-wmiobject win32\_networkadapter|select netconnectionid, name, InterfaceIndex, netconnectionstatus
Copied!
The command and the output from the command appear in the figure that follows.
1
![image045.png](images/image045.png)
Copied!
The difference is that instead of plain text, the command returns objects that can be further manipulated. Therefore, while the above command actually returns the network connection status of all network adapters, the NetSh command only returns the ones that are connected. If I filter on a netconnectionstatus of 2 I can return only the connected network adapters. The command becomes this one (this is a single line command that I broke at the pipeline character for readability):
1
get-wmiobject win32\_networkadapter -filter "netconnectionstatus = 2"|
2
3
selectnetconnectionid,name,InterfaceIndex,netconnectionstatus
Copied!
The command and output appear in the figure that follows.
image047.png
If the desire is to obtain the connection status of more than just network adapters that are connected, then the task will require writing a script to do a lookup. The lookup values appear in the table that follows:
Value
Meaning
0
Disconnected
1
Connecting
2
Connected
3
Disconnecting
4
Hardware not present
5
Hardware disabled
6
Hardware malfunction
7
Media disconnected
8
Authenticating
9
Authentication succeeded
10
Authentication failed
11
Invalid Address
12
Credentials required
The Get-NetworkAdapterStatus.ps1 script requires at least Windows PowerShell 2.0 which means that it will run on Windows XP SP3 and above.
Get-NetworkAdapterStatus.Ps1
1
<#
2
3
.Synopsis
4
Produces a listing of network adapters and status on a local or remote machine.
5
6
.Description
7
This script produces a listing of network adapters and status on a local or remote machine.
8
9
.Example
10
Get-NetworkAdapterStatus.ps1 -computer MunichServer
11
Lists all the network adapters and status on a computer named MunichServer
12
13
.Example
14
Get-NetworkAdapterStatus.ps1
15
Lists all the network adapters and status on local computer
16
17
.Inputs
18
[string]
19
20
.OutPuts
21
[string]
22
23
.Notes
24
NAME: Get-NetworkAdapterStatus.ps1
25
26
AUTHOR: Ed Wilson
27
28
LASTEDIT: 1/10/2014
29
30
KEYWORDS: Hardware, Network Adapter
31
32
.Link
33
34
Http://www.ScriptingGuys.com
35
36
#Requires -Version 2.0
37
38
#>
39
40
Param(
41
[string]$computer=$env:COMPUTERNAME
42
) #end param
43
44
functionGet-StatusFromValue
45
{
46
Param($SV)
47
switch($SV)
48
{
49
0 { " Disconnected" }
50
1 { " Connecting" }
51
2 { " Connected" }
52
3 { " Disconnecting" }
53
4 { " Hardware not present" }
54
5 { " Hardware disabled" }
55
6 { " Hardware malfunction" }
56
7 { " Media disconnected" }
57
8 { " Authenticating" }
58
9 { " Authentication succeeded" }
59
10 { " Authentication failed" }
60
11 { " Invalid Address" }
61
12 { " Credentials Required" }
62
Default { "Not connected" }
63
}
64
65
} #end Get-StatusFromValue function
66
67
# \*\*\* Entry point to script \*\*\*
68
69
Get-WmiObject-Classwin32\_networkadapter-computer$computer|
70
Select-ObjectName, @{LABEL="Status";
71
72
EXPRESSION={Get-StatusFromValue$\_.NetConnectionStatus}}
Copied!
If my environment is Windows 7 and Windows Server 2008 R2, I can use either Windows PowerShell 3.0 or Windows PowerShell 4.0. The advantage here, is that I gain access to the Get-CimInstance cmdlet which uses WinRM for remoting instead of DCOM that the Get-WmiObject cmdlet uses. The only change to the Get-NetworkAdapterStatus.ps1 script that is required is to replace the Get-WmiObject line with Get-CimInstance. The revision appears here:
1
# \*\*\* Entry point to script \*\*\*
2
3
Get-CimInstance-Classwin32\_networkadapter-computer$computer|
4
Select-ObjectName, @{LABEL="Status";
5
6
EXPRESSION={Get-StatusFromValue$\_.NetConnectionStatus}}
Copied!
When I run the Get-StatusFromValue.ps1 script, in the Windows PowerShell ISE, I see the output achieved here.
image049.png

Using the NetAdapter module

On Windows 8 and above the NetAdapter module contains the Get-NetAdapter function. To see the status of all network adapters, use the Get-NetAdapter function with no parameters. The command appears here:
1
Get-NetAdapter
Copied!
The output from this command appears here.
image051.png
I can reduce the output to only physical adapters by using the -physical parameter. This command appears here.
1
Get-NetAdapter-Physical
Copied!
If I only want to see the physical network adapters that are actually up, I pipeline the results to the where-object. This command appears here.
1
Get-NetAdapter-physical|wherestatus-eq'up'
Copied!
The commands and the output from the two previous commands appear in the figure that follows.
image053.png
Last modified 3yr ago