Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Windows Server 2012 R2 Network Cmdlets: Part 4

$
0
0

Summary: Use Windows PowerShell to work with IP addresses in Windows Server 2012 R2.

Hey, Scripting Guy! Question Hey, Scripting Guy!

Now that we have Windows PowerShell cmdlets to use throughout Windows Server 2012 R2, is there any easier way to assign IP addresses to network adapters?

—EL

Hey, Scripting Guy! Answer Hello EL,

Honorary Scripting Guy, Sean Kearney, is here today, unlocking more of the Power of the network cmdlets. This is the fourth part in a series called Windows PowerShell Network Week. You also might enjoy reading:

When I first tried script to set an IP address on a server, it was without good "fiend" (did I say fiend?) netsh.exe.

Actually netsh.exe wasn’t bad, but it was an older way of doing things in the old server world.

I didn’t like working that way because it just wasn’t that consistent. I would learn a little about DSQuery.exe, and then find that I had to relearn things just to use netsh.exe. So for that reason, I wasn’t very productive in the old word of command-line utilities. You had to master each individual utility and its own special syntax.

So with netsh.exe, you used to do something like this to set up your IP address:

NETSH>

NETSH>Interface

NETSH Interface>SHOW INTERFACE

Then quickly grab the pen and paper or Notepad and try to copy the names out of the window. “Good, my network adapter is called “Local Area Connection”.”

Then with that knowledge in mind, assign the IP address:

NETSH Interface>TCP

NETSH Interface tcp>IP

NETSH Interface ipv4>SET address name=”Local Area Connection” static 192.168.1.5 255.255.255.0 192.168.1.1

To be honest, after you had the pattern down, you could script a little line like this for the task:

NETSH interface ipv4 name=”Local Area Connection” static 192.168.1.5 255.255.255.0 192.168.1.1

But then you step into a new scenario with a server with a few network adapters, and perhaps you want to automate it a bit more. Netsh.exe could be automated, but not easily.

Now things have gotten a lot easier. The Windows Powershell cmdlets for creating an IP address just got way easier. First let’s think…with IPconfig, how would you get your IP address if you had a bunch of network adapters? I used to do something like this:

Ipconfig | FIND “IPv4”

That worked OK. I could at least get a list of the information. But what if I wanted to parse that data? Yes, you could do that in DOS and the old world—but not as easily as I would have liked.

In Windows PowerShell, we have the option to pull that information out as an object. This means parsing of that data is far easier to access. To get our IP addresses, we just use this cmdlet:

Get-NetIPAddress

This will dump it out in a nice list or we can choose to view it in a table:

Get-NetIPAddress | Format-Table

Or if I so choose, I can take only what I want out of this list:

Get-NetIPAddress | Select-Object IPAddress, InterfaceAlias

If I need to create a new IP address to be used, I can use the New-NetIPAddress cmdlet. The biggest difference in using this cmdlet over using netsh.exe is that I plug in the number of bits for the subnet mask.

So first we can find our live network adapters. This is something that (as we saw last time in Windows Server 2012 R2 Network Cmdlets: Part 3) was far easier by using Windows PowerShell:

Get-NetAdapter –physical | Where { $_.Status –eq ‘Up’ }

Now if you’re just plugging in a cable, and you want to assign an IP address to the only live network adapter, you can pipe this output directly into New-IPAddress:

To assign the IP address of 192.168.1.5, a subnet mask 255.255.255.0 (a 24-bit prefix), and a default gateway of 192.168.1.1, we could use the following command—and because it’s Windows PowerShell, we can even do it in a safe manner with –whatif:

Get-NetAdapter –physical | Where { $_.Status –eq ‘Up’ } | New-NetIPAddress –Ipaddress 192.168.1.5
–PrefixLength 24 –DefaultGateway 192.168.1.1 –whatif

If you’re satisfied with the results, and you’re confident, you can “Release the Kraken” and get rid of the –whatif:

Get-NetAdapter –physical | Where { $_.Status –eq ‘Up’ } | New-NetIPAddress –Ipaddress 192.168.1.5
–PrefixLength 24 –DefaultGateway 192.168.1.1

Pretty cool eh? We now have an easier way to identify the live network adapters that we want to assign an IP address to on a new server.

EL, take a gander in tomorrow as we play with adding more interesting bits, like DNS settings, to those configurations!

I invite you to follow The Scripting Guys on Twitter and Facebook. If you have any questions, send an email to The Scripting Guys at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then remember eat your cmdlets each and every day with a taste dash of creativity.

Sean Kearney, Windows PowerShell MVP, Honorary Scripting Guy 


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>