Automation of Web Application Creation in Managed Environments (Part IV: Attach IPs to the network adapter)

Attaching an IP to the network adapter is the next part of automation discussed in this series and this article in specific.
With this script we have something special. I developed it on Windows Server 2012 first and then figured out that the “get-netipaddress” and “new-netipaddress” commands are only available on WS2012 and Windows 8.

Okay, well too bad because this one was really so easy.

param (
    [string] $IP

if($IP -ne $null -and $IP -ne "") {
    $base = get-netipaddress | ? { $_.AddressFamily -eq "IPv4" -and $_.PrefixLength -eq 24 } | select-object -first 1
    if($base -ne $null) {
        $newip = New-NetIPAddress -IPAddress $IP -Interfacealias $base.InterfaceAlias -AddressFamily $base.AddressFamily -PrefixLength $base.PrefixLength;
} else {
    Write ("Please provide parameter: 'IP'");

You give the script the IP you want to add, this should be an IPv4…you get the first IP of the machine with the get-netipaddress command and the correct prefixlength (this is basically the subnet mask or 24).

if you find one (hopefully you have one) then you can use the information you have on this first ipaddress (InterfaceAlias, AddressFamily and PrefixLength (which will be 24 of course as we defined that as filter criteria)) in the new-netipaddress command where the IPAddress parameter is the one you want to take note of.

That’s pretty much it for Windows Server 2012.

Now for Windows Server 2008. Good news first. It’s possible, it’s just not as comfy. You need to either use the “netsh interface ipv4 show address” command and parse the resulting string for the connection name of the correct entry but if you have multiple it might be a better idea to use a parameter instead and just give the correct name. You can really use the same name across all servers, right?

param (
    [string] $IP = "",
    [string] $ConnectionName = "Local Area Connection"

$ipconf = ipconfig;
$str = [string]$ipconf;
$Gateway = "Default Gateway . . . . . . . . . : ";
$Subnet = "Subnet Mask . . . . . . . . . . . : ";
$i = $str.indexof($Gateway);
$gw = $str.substring($i + $Gateway.Length, 15).trimend();
$i = $str.indexof($Subnet);
$sn = $str.substring($i + $Subnet.Length, 15).trimend();

#netsh interface ipv4 show address
netsh interface ipv4 add address $ConnectionName $IP $sn
#netsh interface ip set address $ConnectionName static $IP $sn $gw

So i parsed the ipconfig result string for the gateway and the subnet mask because this will not just be the prefixlength but the subnet mask which will probably be again, so you might just want to use a static string if you don’t like this solution (…i don’t love it).

So you can see the similarity of the core command here with “netsh interface ipv4 add address” and “new-netipaddress”. That’s where the “magic” happens. You have the option to use set address as well to just replace the base one but then you may also add the default gateway, which is why I have it in the code. The key here is that it’s all based on the existing connection so it’s all smooth sailing.

That’s it. This one wasn’t too much of a time waster. But it’s a good script to have. Wikipedia explains a bit more and Thomas Maurer has a good comparison on the two different approaches.

So why would you want to automate it in the first place? Well it’s again a UI thing with so many clicks and you add most of the information again and again like the subnet mask. So it’s well worth the time of writing the script and then just running it multiple times or using an input file and the get-content command as a loop.

Why is this it’s own step? It doesn’t necessarily need to be it’s own step. It could be done with the bindings in IIS but you want to make sure the IPs are available for that step so it might just be a good idea to keep them separate.

Continue Reading…Part V

Back To Part III
Back To Overview


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

<span>%d</span> bloggers like this: