PowerShell Function Set-IPAddress

August 13, 2008 at 3:08 PMAndy Schneider

I recently came across the need to configure IP information on a Network interface using a script. This is definitely something that can be accomplished with NETSH,  but I was looking for a way to do it with native PowerShell using Get-WmiObject. If there is a choice, I tend to prefer to stick with native Powershell Cmdlets and functions rather than running cmd based tools, but that is just my personal opinion and you can take it or leave it.

My goal was to be able to specify a Network Interface by name, ie "Local Area Connection" and pass it an IP address, mask, gateway, and 1 or 2 DNS servers. In order to accomplish this there were two WMI classes that I needed. The first is Win32_NetworkAdapter. The second is Win32_NetworkAdapterConfiguration. The real key (pun intended) was to figure out a way to join the information from these two classes so that I knew for sure that I was working with the same network interface. It turns out that both classes have a property called InterfaceIndex.

Win32_NetworkAdapter has a property called netconnectionid which is equal to the name of the Adapter, ie "Local Area Connection".

With that information, here is a function that you can use to specify settings for a NIC based on the name of the NIC.

   1: function Set-IPAddress {
   2:         param(  [string]$networkinterface,
   3:                 [string]$ip,
   4:                 [string]$mask,
   5:                 [string]$gateway,
   6:                 [string]$dns1,
   7:                 [string]$dns2,
   8:                 [string]$registerDns = "TRUE"
   9:          )
  10:         
  11:                 
  12:         #Start writing code here
  13:         $dns = $dns1
  14:         if($dns2){$dns ="$dns1,$dns2"}
  15:         $index = (gwmi Win32_NetworkAdapter | where {$_.netconnectionid -eq $networkinterface}).InterfaceIndex
  16:         $NetInterface = Get-WmiObject Win32_NetworkAdapterConfiguration | where {$_.InterfaceIndex -eq $index}
  17:         $NetInterface.EnableStatic($ip, $mask)
  18:         $NetInterface.SetGateways($gateway)
  19:         $NetInterface.SetDNSServerSearchOrder($dns)
  20:         $NetInterface.SetDynamicDNSRegistration($registerDns)
  21:         
  22: }

Hope you find this useful.

Andy

Posted in:

Tags:

Comments (8) -

Andy, awesome script.  Any chance I could convince you to start uploading these to http://poshcode.org?

Reply

When you need to filter WMI, you need to use the -filter parameter, it'll speed up up drastically, especially when you run it against remote machines. Line 16 should be:

$NetInterface = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "Index=$index"

Reply

Thanks Joel. That's a great point.

Andy

Reply

Byron Boudreaux
Byron Boudreaux says:

This is a great script that I used to combine with another script that reads the DHCP assigned IP information of a box, sets the various items to variables (instead of taking string inputs) and then sets them statically as the script does above.  The one problem I am having is setting the DNSServerSearchOrder.  I am able to read the information into variables but I cannot right them back using the method in the script above.

Reply

Shane Powser
Shane Powser says:

The SetDNSServerSearchOrder is not working because it expects a string array and not a comma seperated list of values.  I changed

if($dns2){$dns ="$dns1,$dns2"}

to

if($dns2){$dns = "$dns1","$dns2"}

Reply

Thanks for this,  just want to let u know I subscribed to ur blog too....thnks =))

Reply

Ester Birne
Ester Birne says:

Hi i have a Windows 7 x86 Computer throwing this error when i execute set-ipaddress, while its working fine on a other one...
any one have a idea why this could be?


Exception calling "EnableStatic" : ""
At C:\Users\theuser\SetIP.ps1:22 char:36
+          $NetInterface.EnableStatic <<<< ($ip, $mask)
    + CategoryInfo          : NotSpecified: (Smile [], MethodInvocationException
    + FullyQualifiedErrorId : WMIMethodException

Reply

how attain I go about making a chocolate milkshake? I allow chocolate ice-cream, and also cow's milk, that's most the ingrediants I recognise lol. you don't have on banana's but Im just wondering what ingrediants accomplish I put on into The blender how to create a delicious chocolate milkshake?

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading