Powershell: Configure a static IP on an unplugged NIC

5,225

Solution 1

You need to remove the existing DHCP IP address already assigned to the adapter. You should also set the DNS server for the interface. I provided an example below, but replace the xxx.xxx.xxx.xxx with your DNS Server IP Address.

You'll need to disable DHCP in the registry for this interface in the PersistentStore before you can set the IP address.

Set-ItemProperty -Path “HKLM:\SYSTEM\CurrentControlSet\services\Tcpip\Parameters\Interfaces\$((Get-NetAdapter -InterfaceIndex 10).InterfaceGuid)” -Name EnableDHCP -Value 0
Remove-NetIpAddress -InterfaceIndex 10 -AddressFamily IPv4
Remove-NetRoute -InterfaceIndex 10 -AddressFamily IPv4 -Confirm:$false
New-NetIpAddress -InterfaceIndex 10 -IpAddress 192.168.9.10 -PrefixLength 24 -DefaultGateway 192.168.9.1 -AddressFamily IPv4
Set-DnsClientServerAddress -InterfaceIndex 10 -ServerAddresses "xxx.xxx.xxx.xxx"

This website has a good example and explanation of the process: https://www.pdq.com/blog/using-powershell-to-set-static-and-dhcp-ip-addresses-part-1/

This website talks about the same problem you're having and their solution: http://www.darrylvanderpeijl.com/inconsistent-parameters-policystore-persistentstore-and-dhcp-enabled/

Solution 2

I had issues on windows 10 1909 with all code I found, it seemed inconsistent. The below code works for me, choose netsh or powershell exclusive code:

$IP = $builddata.templateIP
$SubnetMaskBits = $builddata.Subnetmaskbits
$SubnetMask = $builddata.Subnetmask
$Gateway = $builddata.gateway
$Dns = $builddata.DNS1,$builddata.DNS2

$IPType = "IPv4"
$adapter = Get-NetAdapter | Where-Object {$_.InterfaceDescription -like "vmx*"} #looking for vmxnet3

# you can use either at this point
# via netsh

netsh interface ipv4 set interface $adapter.InterfaceIndex dadtransmits=0 store=persistent
netsh interface ip set address name="$($adapter.name)" static $IP $SubnetMask $Gateway 1

# via PowerShell

$adapter | Get-NetIPInterface | ? {$_.addressfamily -eq  $IPType} | % {Get-NetIPAddress | Remove-NetIPAddress -Confirm:$false}
$adapter | Get-NetIPInterface | New-NetIPAddress `
     -AddressFamily $IPType `
     -IPAddress $IP `
     -PrefixLength $SubnetMaskBits `
     -DefaultGateway $Gateway -Confirm:$false
Share:
5,225

Related videos on Youtube

Xelorz
Author by

Xelorz

Updated on September 18, 2022

Comments

  • Xelorz
    Xelorz over 1 year

    I'm trying to find a way to configure a permanent static IP on Windows 10 IOT devices with their Nic unplugged. I have a script that needs to be run on the devices before they're in place and part of it is configuring the NICs. When I use the following: New-NetIpAddress -InterfaceIndex 10 -IpAddress 192.168.9.10 -PrefixLength 24 -DefaultGateway 192.168.9.1 -AddressFamily IPv4 I get an error New-NetIpAddress : Inconsistent parameters PolicyStore PersistentStore and Dhcp Enabled I attempted to explictly turn off DHCP with Set-NetIPInterface -InterfaceIndex 10 -Dhcp Disabled before ussing the New-NetIpAddress command but I get the same error.

    Any suggestions?

  • Tim Liston
    Tim Liston over 5 years
    Your code failed, or this code fails on an unplugged adapter?
  • Xelorz
    Xelorz over 5 years
    I copy and pasted the code onto the device using 8.8.8.8 as the dns. Same Error: Inconsistent parameters PolicyStore PersistentStore and Dhcp Enabled
  • Xelorz
    Xelorz over 5 years
    I googled for hours and never came across anything that suggested that. Thanks.
  • Tim Liston
    Tim Liston over 5 years
    Glad I could help. If it worked for you then maybe accept this as the answer to help someone else in the future. [stackoverflow.com/help/someone-answers]