Identifying networking cmdlets
In Windows Server 2012, there are now hundreds of Windows
PowerShell cmdlets that can be used to view, configure, and monitor
different networking components and services in the platform. The
tasks you can perform using these cmdlets range from the common (such
as configuring static IP addresses or DHCP reservations for servers)
to more specialized (such as configuring quality-of-service
parameters) to settings related to virtual environments (such as
configuring the Hyper-V extensible switch). So let’s begin with a more practical approach to the
problem of administering a Windows Server 2012 networking environment
using Windows PowerShell by asking a simple question: How can you find
the right cmdlet (if there is a cmdlet) to perform a particular
networking task?
You could start by using the Get-Command cmdlet to search for
all Windows PowerShell cmdlets and functions that have the string
“net” in their names. This generates a lot of output, however, as
shown here:
PS C:\> Get-Command *net*
CommandType Name ModuleName
----------- ---- ----------
Function Add-NetIPHttpsCertBinding NetworkTransition
Function Add-NetLbfoTeamMember NetLbfo
Function Add-NetLbfoTeamNic NetLbfo
Function Add-NetSwitchTeamMember NetSwitchTeam
Function Copy-NetFirewallRule NetSecurity
Function Copy-NetIPsecMainModeCryptoSet NetSecurity
Function Copy-NetIPsecMainModeRule NetSecurity
Function Copy-NetIPsecPhase1AuthSet NetSecurity
Function Copy-NetIPsecPhase2AuthSet NetSecurity
Function Copy-NetIPsecQuickModeCryptoSet NetSecurity
Function Copy-NetIPsecRule NetSecurity
Function Disable-NetAdapter NetAdapter
Function Disable-NetAdapterBinding NetAdapter
Function Disable-NetAdapterChecksumOffload NetAdapter
Function Disable-NetAdapterEncapsulatedPacketTaskOffload NetAdapter
Function Disable-NetAdapterIPsecOffload NetAdapter
...
From the preceding output, you can see there are several
Windows PowerShell modules that perform network-related actions. To
see this more clearly, the following commands take the preceding
output, sort it by module name, and remove duplicates:
PS C:\> Get-Command *net* | Sort-Object ModuleName | Format-Table ModuleName `
-HideTableHeaders | Out-String | Out-File c:\data\test.txt
PS C:\> Get-Content C:\data\test.txt | Get-Unique
ActiveDirectory
BranchCache
DnsServer
MsDtc
NetAdapter
NetConnection
NetLbfo
NetQos
NetSecurity
NetSwitchTeam
NetTCPIP
NetworkTransition
NFS
SmbShare
To investigate the NetTCPIP module further, you can use the
–Module parameter of Get-Command to list all
cmdlets and functions contained in this module:
PS C:\> Get-Command -Module NetTCPIP | Sort-Object Name | Format-Table Name
Name
----
Get-NetIPAddress
Get-NetIPConfiguration
Get-NetIPInterface
Get-NetIPv4Protocol
Get-NetIPv6Protocol
Get-NetNeighbor
Get-NetOffloadGlobalSetting
Get-NetPrefixPolicy
Get-NetRoute
Get-NetTCPConnection
Get-NetTCPSetting
Get-NetTransportFilter
Get-NetUDPEndpoint
Get-NetUDPSetting
New-NetIPAddress
New-NetNeighbor
New-NetRoute
New-NetTransportFilter
Remove-NetIPAddress
Remove-NetNeighbor
Remove-NetRoute
Remove-NetTransportFilter
Set-NetIPAddress
Set-NetIPInterface
Set-NetIPv4Protocol
Set-NetIPv6Protocol
Set-NetNeighbor
Set-NetOffloadGlobalSetting
Set-NetRoute
Set-NetTCPSetting
Set-NetUDPSetting
At this point, you can begin using Get-Help to learn about the
syntax of NetTCPIP cmdlets you’re interested in and to see some
examples of their usage. Unfortunately for administrators who are
not that familiar with Windows PowerShell, the syntax displayed when
you use Get-Help with a cmdlet can appear daunting. For example,
consider a scenario where you have a web server running Windows
Server 2012 and you want to add a second IP address to a network
adapter on the server.
You might guess from the output of Get-Command
–Module NetTCPIP shown previously that New-NetIPAddress
is the cmdlet you use to perform this task, and you would be
correct. But to the Windows PowerShell beginner, the syntax from
Get-Help New-NetIPAddress might look quite
confusing:
Parameter Set: ByInterfaceAlias
New-NetIPAddress -InterfaceAlias <String> [-AddressFamily <AddressFamily> ] [-AsJob]
[-CimSession <CimSession[]> ] [-DefaultGateway <String> ] [-IPv4Address <String> ]
[-IPv6Address <String> ] [-PassThru] [-PreferredLifetime <TimeSpan> ]
[-PrefixLength <Byte> ] [-PrefixOrigin <PrefixOrigin> ] [-SkipAsSource <Boolean> ]
[-Store <Store> ] [-SuffixOrigin <SuffixOrigin> ] [-ThrottleLimit <Int32> ]
[-Type <Type> ] [-ValidLifetime <TimeSpan> ] [-Confirm] [-WhatIf] [ <CommonParameters>]
Parameter Set: ByIfIndexOrIfAlias
New-NetIPAddress [-AddressFamily <AddressFamily> ] [-AsJob]
[-CimSession <CimSession[]> ] [-DefaultGateway <String> ] [-InterfaceAlias <String> ]
[-InterfaceIndex <UInt32> ] [-IPv4Address <String> ] [-IPv6Address <String> ]
[-PassThru] [-PreferredLifetime <TimeSpan> ] [-PrefixLength <Byte> ]
[-PrefixOrigin <PrefixOrigin> ] [-SkipAsSource <Boolean> ] [-Store <Store> ]
[-SuffixOrigin <SuffixOrigin> ] [-ThrottleLimit <Int32> ] [-Type <Type> ]
[-ValidLifetime <TimeSpan> ] [-Confirm] [-WhatIf] [ <CommonParameters>]
Fortunately, the new Show-Command cmdlet in Windows Server
2012 can help make the syntax of Windows PowerShell cmdlets easier
to understand and use. Start typing the following command:
PS C:\> Show-Command New-NetIPAddress
When you run the preceding command, the properties page shown
in Figure 1 opens
to show you the different parameters you can use with the
New-NetIPAddress cmdlet. Parameters such as
InterfaceAlias and
IPAddress that are marked with an asterisk are
mandatory; those not marked this way are optional.
Clearly, to add a new IP address you first need to know the
alias or index of the network interface to which you want to add the
address. To find the interfaces on the system, you could use Get-Command *interface* to find all cmdlets
that include “interface” in their name. Of the eight cmdlets
displayed when you run this command, the cmdlet Get-NetIPInterface
is the one you are looking for, and running this cmdlet displays a
list of all interfaces on the server:
PS C:\> Get-NetIPInterface
ifIndex InterfaceAlias AddressFamily NlMtu(Bytes) InterfaceMetric Dhcp
------- -------------- ------------- ------------ --------------- ----
12 Ethernet IPv6 1500 5 Disabled
14 Teredo Tunneling Pseudo... IPv6 1280 50 Disabled
13 isatap.{4B8DC8AE-DE20-4... IPv6 1280 50 Disabled
1 Loopback Pseudo-Interfa. IPv6 4294967295 50 Disabled
12 Ethernet IPv4 1500 5 Disabled
1 Loopback Pseudo-Interfa. IPv4 4294967295 50 Disabled
From the preceding command output, you can see that the
interface you are looking for is identified by the alias “Ethernet.”
To view the existing TCP/IP configuration of this interface, you can
use the –InterfaceAlias with the
Get-NetIPAddress cmdlet as follows:
PS C:\> Get-NetIPAddress -InterfaceAlias Ethernet
IPAddress : fe80::cf8:11a1:2e3:d9bc%12
InterfaceIndex : 12
InterfaceAlias : Ethernet
AddressFamily : IPv6
Type : Unicast
PrefixLength : 64
PrefixOrigin : WellKnown
SuffixOrigin : Link
AddressState : Preferred
ValidLifetime : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource : False
PolicyStore : ActiveStore
IPAddress : 172.16.11.236
InterfaceIndex : 12
InterfaceAlias : Ethernet
AddressFamily : IPv4
Type : Unicast
PrefixLength : 24
PrefixOrigin : Manual
SuffixOrigin : Manual
AddressState : Preferred
ValidLifetime : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource : False
PolicyStore : ActiveStore
The preceding command output shows that the Ethernet interface
currently has 172.16.11.236/24 as its IPv4 address and Classless
Inter-Domain Routing (CIDR) prefix.
Returning to the open properties page displayed by Show-Command New-NetIPAddress, you can add a
second IP address to the interface by specifying the parameter
values shown in Figure 2.
If you click Copy in the properties page shown in Figure 2, the command
is copied to the clipboard. The resulting command looks like
this:
New-NetIPAddress -InterfaceAlias Ethernet -IPAddress 172.16.11.237 `
-AddressFamily IPv4 -PrefixLength 24
If you click Run, the command executes. By using
–InterfaceAlias with the Get-NetIPAddress
cmdlet again, you can verify that the command accomplished the
desired result:
PS C:\> Get-NetIPAddress -InterfaceAlias Ethernet
IPAddress : fe80::cf8:11a1:2e3:d9bc%12
InterfaceIndex : 12
InterfaceAlias : Ethernet
AddressFamily : IPv6
Type : Unicast
PrefixLength : 64
PrefixOrigin : WellKnown
SuffixOrigin : Link
AddressState : Preferred
ValidLifetime : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource : False
PolicyStore : ActiveStore
IPAddress : 172.16.11.237
InterfaceIndex : 12
InterfaceAlias : Ethernet
AddressFamily : IPv4
Type : Unicast
PrefixLength : 24
PrefixOrigin : Manual
SuffixOrigin : Manual
AddressState : Preferred
ValidLifetime : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource : False
PolicyStore : ActiveStore
IPAddress : 172.16.11.236
InterfaceIndex : 12
InterfaceAlias : Ethernet
AddressFamily : IPv4
Type : Unicast
PrefixLength : 24
PrefixOrigin : Manual
SuffixOrigin : Manual
AddressState : Preferred
ValidLifetime : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource : False
PolicyStore : ActiveStore
Opening the Advanced TCP/IP Settings for the interface from
the Network Connections folder confirms the result. (See Figure 3.)