Wednesday, June 17, 2015

Managing Windows Server 2012 R2 Using PowerShell : Part-5 : Setting IP Address of the Server.

 

  1. Part 1 : Windows Server 2012 R2 Installation.
  2. Part 2 : Exploring PowerShell Default Settings.
  3. Part 3 : Getting and setting server name.
  4. Part 4 : Getting IP Address of the server.

Configuration

Part - 5 { Configuration - IP Addresses}

In our previous blog post, we seen that how can we check current IP-Address of the  Windows 2012 R2 server by using PowerShell.

In this blog post, we are going to set an IP-Address by using PowerShell and believe me it's very easy to do.

We are going to set the below network IP configuration to our Ethernet card. Please note, the below IP address are not  connected to internet in my environment, I am using them for demo purpose only, you can use real IP addresses and real DNS server IP according to your needs..

Desired Network Configuration

IP Address

192.168.1.100

Subnet mask

255.255.255.0

Default Gateway

192.168.1.200

DNS Server 1

8.8.8.8

DNS Server 2

202.56.215.55

 

Setting the IP-Address ( the non-PowerShell way ).

There are very few ways to set  static IP-Addresses. The first way is by setting it manually, the second way is by using Netsh.exe command, and the third way by using WMI.

  1. Manually.
    1. Comeon! I know, that you know, how to do it.
  2. Using WMI.
    1. check this cool old blog post on, how to set static IP address using WMI , ( it's PowerShell , but, you can use WMI without PowerShell too.) (http://blogs.technet.com/b/heyscriptingguy/archive/2012/02/28/use-powershell-to-configure-static-ip-and-dns-settings.aspx) 
  3. Netsh.exe
    1. Let's try to set Static IP and DNS Server by using Netsh.exe
    2. In the below screenshot, you can see , that currently, by Ethernet card is set to get an IP from a DHCP server.
    3. 6-17-2015 10-48-29 AM
    4. let's run the netsh command to set static IP address on Ethernet card, whose name is "Ethernet"
    5. netsh interface ip set "Ethernet" static 192.168.1.100 255.255.255.0 192.168.1.200
    6. 6-17-2015 11-04-58 AM
    7. In the above screenshot, you can see, that our above command, has run successfully.
    8. Now we , need to add the DNS server name. We use the  Netsh.exe command to do the same. You see error, in the output of the command, in my-case, just ignore it, because, I am not connected to internet.
    9. netsh interface ip add dns "Ethernet" 8.8.8.8
    10. netsh interface ip add dns "Ethernet" 8.8.8.8 index=2
    11. We run the above command two times, first one is setting a Primary DNS server and the second command is setting Secondary DNS server.
    12. 6-17-2015 11-28-22 AM 
    13. Now. let's see the result.
    14. In the below screenshot you can see that, Netsh.exe command, sets the static IP and DNS server entries successfully.
    15. 6-17-2015 11-28-39 AM

But, this is a PowerShell blog, we must learn to do the same by using PowerShell, and in a PowerShell way, let's do it.

Setting the IP-Address ( The PowerShell way ).

In above section, we have seen the various ways for setting static IP address, there are always more than one way to do the same thing, but I suggest you to choose the PowerShell way, it's is easy.

The first thing, which we need to know, the name of our Ethernet adapter and its' ifindex ( interface index), so that, in-case we have multiple network adapters on the server, we  can choose the right one.

To check the list of all network adapters, we run the Get-NetAdapter cmdlet.

In the below screenshot you can see, that currently, I have only one network adapter installed on my server, the name of the Lan adapter is "Ethernet" and it's interface index is 12.

6-17-2015 10-49-27 AM

Now,before setting static IP address, we need to know,if the current IP address on the server is static or allocated by DHCP server.

To check this, we use the cmdlet name Get-NetIPAddress. I want to see details about only one specific Ethernet cards, so in the below command, I am  running Get-NetIPAdress and in -InterfaceIndex we have give the index number of our desired LAN adapter.

Get-NetIPAddress -InterfaceIndex 12

6-17-2015 10-51-14 AM

In the above screenshot, you can see that, our PrefixOrigin and SuffixOrigin is from DHCP server, which means, we are getting IP addresses from the DHCP server.

Setting IP Address.

We use the New-NetIPAddress cmdlet to set a new static IP.

Usage of cmdlet is simple. in the below command, we are setting a new IP, Gateway, PrefixLenght ( Subnet mask) to our Ethernet adapter whose name is "Ethernet" and we are setting IP version 4.

New-NetIPAddress -InterfaceAlias "Ethernet" -IPAddress 192.168.1.100 -DefaultGateway 192.168.1.200 -PrefixLength 24 -AddressFamily IPv4

6-17-2015 11-34-08 AM

In the above command, you can see that our command has run successfully.

Now, out next task is to set DNS Server name, to set DNS server names, we use the Set-DnsClientServerAddress cmdlet. In the below command we are setting Primary and Secondary DNS server name entries, you can notice in the below command, I have used the -InterfaceIndex parameter to set the entries, if you want, you can use the -InterfaceAlias parameter.

Set-DnsClientServerAddress -InterfaceIndex 12 -ServerAddresses 8.8.8.8,202.56.215.55

 

6-17-2015 4-27-04 PM

In the above screenshot, you can see that our above command runs successfully, let's cross-check it.

In the below screenshot you can see, that over all above commands runs successfully, and we manage to set static IP to our Ethernet adapter,

6-17-2015 11-36-04 AM

All done, see you in next blog post.

Aman Dhally Manya Kaur

Regards.
Aman Dhally
If you like, you can follow me on Twitter and Facebook. You can also check my “You Tube channel for PowerShell video tutorials. You can download all of my scripts from “Microsoft TechNet Gallery”.

 


 


 


 


 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.