powershell-3.0nicdynamic-ip

Assigning Multiple ips to a NIC using powershell


I am trying to assign multiple ips to a NIC on a windows server. Is there any way I could dynamically generate the ip addresses and assign it to the NIC


Solution

  • You want to call the EnableStatic method on the instance of the Win32_NetworkAdapterConfiguration WMI class for the network interface you want to configure.

    uint32 EnableStatic(
      [in]  string IPAddress[],
      [in]  string SubnetMask[]
    );
    

    You can see above it takes two parameters. A string array of IP addresses and a string array of subnet masks.

    It will return an status code. 0 indicates success.

    Here is PowerShell example code:

    Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled=true" | 
        ForEach-Object {
            $result = $_.EnableStatic(("192.168.1.10","10.0.0.10"),("255.255.255.0","255.0.0.0"))
            if ($result -ne 0) {
                # handle non-successful response code here.
            }
        }