Windows Server 2012 Automation with PowerShell Cookbook
上QQ阅读APP看书,第一时间看更新

Configuring static networking

TCP/IP is the primary technology used for communicating between computers today. When first building out an environment, one of the first items to accomplish is to define and apply an IP addressing scheme. Once the addressing scheme is defined, we can create static addresses for our first servers. Later, we will configure DHCP in case static addressing is not desired for all of the systems in your environment.

Getting ready

From the following diagram we can see that we have already defined our addressing scheme using both IPv4 and IPv6. At the start of our network, we have a router acting as a default gateway, and we will configure two servers in preparation for becoming domain controllers. The default gateway router is already statically assigned with IPv4 and IPv6 addresses:

Getting ready

All three of these components are connected to a common Ethernet segment to communicate with each other.

Note

Before defining any networking configuration, we should confirm that our addresses do not conflict with other networks in our environment. Even when building out isolated environments, it is best to use different network addresses in case of accidental conflict with production environments.

How to do it...

Carry out the following steps to configure static networking:

  1. Find the interface to set by executing Get-NetIPInterface:
    How to do it...
  2. Set the IP information using New-NetIPAddress:
    New-NetIPAddress -AddressFamily IPv4 -IPAddress 10.10.10.10 -PrefixLength 24 -InterfaceAlias Ethernet 
    How to do it...
  3. Set DNS Servers using Set-DnsClientServerAddress:
    Set-DnsClientServerAddress -InterfaceAlias Ethernet -ServerAddresses "10.10.10.10","10.10.10.11" 
    How to do it...
  4. Set the default route using New-NetRoute:
    New-NetRoute -DestinationPrefix "0.0.0.0/0" -NextHop "10.10.10.1" -InterfaceAlias Ethernet 
    How to do it...

How it works...

In the first step we list out the network adapters available on the server. Windows Servers often include several network adapters of different types, and depending on the features installed, there can be several more. By executing Get-NetworkIPInterface, we list the interface names and indexes that we will use to identify the specific interface we desire to configure.

The second and third steps use New-NetIPAddress and Set-DnsClientServerAddress to configure the identified interface with IPv4 address and DNS targets for the specified interface.

The last step uses New-NetRoute to define a network route. The –DestinationPrefix 0.0.0.0/0 parameter identifies this route as the default route, or default gateway. The –NextHop 10.10.10.1 parameter is the router address to forward traffic into if another route does not take precedence.

The following screenshot shows the IPv4 address properties after finalizing configuration via PowerShell:

How it works...

There's more...

There are a few more features provided by PowerShell. They are as follows:

  • IPv6 addressing: In addition to configuring IPv4, PowerShell can also configure IPv6 addresses. The process for configuring static IPv6 addressing is exactly the same as IPv4, the only change is the addresses themselves.

    Following are examples of configuring IPv6 on the same host. Note that both IPv4 and IPv6 addressing can coexist on the same server without issue:

    New-NetIPAddress -AddressFamily IPv6 -IPAddress 2001:db8:1::10 `
    -PrefixLength 64 -InterfaceAlias Ethernet
    New-NetRoute -DestinationPrefix ::/0 -NextHop 2001:db8:1::1 `
    -InterfaceAlias Ethernet
    Set-DnsClientServerAddress -InterfaceAlias Ethernet `
    -ServerAddresses "2001:db8:1::10","2001:db8:1::11"
    There's more...
  • Additional IP addresses: By using the New-NetIPAddress function, an interface can be configured with multiple IP addresses simultaneously. This configuration is often used for clustering or load balancing within Windows. Following is an example of configuring an additional address:
    New-NetIPAddress -AddressFamily IPv4 -IPAddress 10.10.10.250 -PrefixLength 24 -InterfaceAlias Ethernet 
    There's more...
  • Additional routes: Windows has the ability to route network packets to more locations than the default gateway. Say for instance, there are two routers on your network: the default gateway and a second gateway. The second gateway is used to access the 10.10.20.0/24 network, and the Windows server needs to be configured to route to it:
    There's more...

    By executing the New-NetRoute command again, with the -DestinationPrefix and -NextHop addresses changed appropriately, we add a specific route to the server:

    New-NetRoute -DestinationPrefix "10.10.20.0/24" -NextHop "10.10.10.254" -InterfaceAlias Ethernet 

    Tip

    In some cases, such as a dedicated management network, the secondary network may be connected to a different network interface. If that is the situation, change the –InterfaceAlias parameter to target the second interface.

    There's more...

    The full list of routes can be viewed by running Get-NetRoute. This will return all IPv4, IPv6, default, and static routes that are defined on the system:

    There's more...