Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell with TCP/IP.
Microsoft Scripting Guy, Ed Wilson, is here. This morning, I decided to make a cup of orange spice tea. I did not even do it manually. I used a tea bag. I know, I know, I know. But hey, sometimes I like the convenience of opening a tea bag and filling the cup with hot water. Boom! I am done. This is especially true when I have a good bag of tea—not some of the cheaper tea that one might purchase at a big-box discount store that sells everything from car batteries to peanut butter. Indeed, over the years, I have found that there are some companies that put high quality tea leaves in their little bags.
The same might be said of a TCP/IP configuration. I mean, I might enjoy writing a script that calls a variety of WMI classes to manually configure the TCP/IP configuration of my workstation—but hey, it is certainly easier to use DHCP. In fact, when the results are the same, there is no reason to waste my time doing things manually. It is the end result that is the goal. Like a fine cup of hot tea, results count.
So whenever DHCP is available, I use it. There are times when I resort to my scripts, but that is generally for test environments when I do not want to take the time to configure a DHCP server. The same is basically true for finding the results—I can use the Get-NetIPAddress function from the NetTCPIP module to retrieve my IP configuration information. This is great because the function returns an object that is easily manipulated. In addition, I do not need to remember esoteric, confusing switches and parameters. Instead, because it is Windows PowerShell, everything is consistent.
To find functions that provide information, I can use the Get-Command cmdlet. I am interested in the verb Get and the module NetTcpIp. Here is the command and the output from the command:
PS C:\> gcm -Verb get -Module NetTCPIP
CommandType Name ModuleName
———– —- ———-
Function Get-NetCompartment NetTCPIP
Function Get-NetIPAddress NetTCPIP
Function Get-NetIPConfiguration NetTCPIP
Function Get-NetIPInterface NetTCPIP
Function Get-NetIPv4Protocol NetTCPIP
Function Get-NetIPv6Protocol NetTCPIP
Function Get-NetNeighbor NetTCPIP
Function Get-NetOffloadGlobalSetting NetTCPIP
Function Get-NetPrefixPolicy NetTCPIP
Function Get-NetRoute NetTCPIP
Function Get-NetTCPConnection NetTCPIP
Function Get-NetTCPSetting NetTCPIP
Function Get-NetTransportFilter NetTCPIP
Function Get-NetUDPEndpoint NetTCPIP
Function Get-NetUDPSetting NetTCPIP
Using Get-NetIPAddress function
From the previous list, I decide I want to use the Get-NetIPAddress function to explore the configuration of my network adapters. Unfortunately these days, a single laptop may have more than a dozen things that are considered network adapters. In fact, most are virtual of some sort. But nearly always, there are at least two real network adapters: wireless and wired. Depending on what is going on, one or both of the adapters may be bound to TCP/IP and may have assigned IP addresses.
At the most basic, I simply type the function name, and it returns information. This is shown here:
As I look over this output, I see that there is IPv6 in additon to IPv4. I am interested in IPv4.
Note For performance reasons, if at all possible, always filter to the left of the Windows PowerShell pipeline,
not to the right.
To filter only IPv4, I could pipe the output to Where-Object. The command might look something like the following:
Get-NetIPAddress | where addressfamily -eq 'ipv4'
But that is a lot of work, and it violates the rule of trying to filter on the left side of the Windows PowerShell pipeline character if at all possible. In this case, the Get-NetIPAddress function has a parameter that will accomplish the same thing as the previous command. Here is the revision:
Get-NetIPAddress -AddressFamily IPv4
The cool thing is that I do not have to type the previous command completely. This is because the Windows PowerShell ISE command completion feature pops up with suggestions. This is shown here:
I simply need to select the appropriate choice from the list to complete my command.
So now I have only IPv4 addresses. But I am also interested in the addresses that are supplied via DHCP. One way of obtaining this information is to look at the PrefixOrigin parameter. The revised command is shown here:
Get-NetIPAddress -AddressFamily IPv4 -PrefixOrigin Dhcp
As shown here, if I am interested in statically assigned information, I can switch from DHCP to Manual:
Get-NetIPAddress -AddressFamily IPv4 -PrefixOrigin Manual
By using the various switches and parameters of the Get-NetIpAddress function, I can easily find a lot of information about my computer. And if I was inclined, I could also obtain information remotely by using the CimSession parameter. All I would need to do is to create one or more CIM sessions and then feed that to the function. Piece of cake.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at firstname.lastname@example.org, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy