官术网_书友最值得收藏!

Configuring IP addressing

By default, Windows uses DHCP to configure any NICs that are found during the installation process. Once you complete the installation of Windows, you can use the settings application netsh.exe, or, of course, PowerShell to set IP configuration manually.

Getting ready

This recipe runs on SRV2.Reskit.Org. This host is a domain-joined system with an NIC that is initially set up to be configured from DHCP.

How to do it...

  1. Get existing IP address information for SRV2:
    $IPType = 'IPv4'
    $Adapter = Get-NetAdapter |
      Where-Object Status -eq 'Up'    
    $Interface = $Adapter |
      Get-NetIPInterface -AddressFamily $IPType
    $IfIndex = $Interface.ifIndex
    $IfAlias = $Interface.Interfacealias
    Get-NetIPAddress -InterfaceIndex $Ifindex -AddressFamily $IPType
  2. Set the IP address for SRV2:
    $IPHT = @{
      InterfaceAlias = $IfAlias
      PrefixLength   = 24
      IPAddress      = '10.10.10.51'
      DefaultGateway = '10.10.10.254'
      AddressFamily  = $IPType
    }
    New-NetIPAddress @IPHT | Out-Null
  3. Set the DNS server details:
    $CAHT = @{
      InterfaceIndex  = $IfIndex
      ServerAddresses = '10.10.10.10'
    }
    Set-DnsClientServerAddress @CAHT
  4. Test the new configuration:
    Get-NetIPAddress -InterfaceIndex $IfIndex -AddressFamily IPv4
    Test-NetConnection -ComputerName DC1.Reskit.Org
    Resolve-DnsName -Name SRV2.Reskit.Org -Server DC1.Reskit.Org |
      Where-Object Type -eq 'A'

How it works...

In step 1, you examined the current IP configuration for SRV2, which looks like this:

In step 2, you set a static IP address for the NIC in SRV2, which produces no output. In step 3, you set the DNS server IP address, which also produces no output.

In step 4, you tested the new IP configuration, which looks like this:

There's more...

In step 1, you used the Get-NetIPConfiguration cmdlet. Two other closely related cmdlets that were not shown in the recipe are Get-NetIPInterface and Get-NetAdapter. Both provide additional information about the network adapter/network interface.

In step 4, you checked the IP configuration by using Get-NetIPAddress to show the IP address and subnet mask. You could have used the Get-NetIPConfiguration cmdlet to return the IP address and subnet mask, plus details of the default gateway and your DNS server IP address.

主站蜘蛛池模板: 聊城市| 东乡族自治县| 镇沅| 青阳县| 县级市| 江西省| 濮阳市| 乌审旗| 安平县| 望城县| 五大连池市| 和田县| 安塞县| 黑河市| 赤水市| 浦东新区| 中江县| 革吉县| 澜沧| 老河口市| 涟源市| 洪雅县| 荆州市| 阳西县| 泸西县| 郑州市| 澎湖县| 东乌| 贵州省| 酉阳| 禄丰县| 长岭县| 平罗县| 吐鲁番市| 怀来县| 视频| 淄博市| 卓资县| 延津县| 永登县| 无为县|