简体   繁体   中英

Set Static IP Address on all Physical (Wired) Adapters

Using PowerShell, we can easily set a static IP address on an adapter where the InterfaceIndex is known like this:

New-NetIPAddress -InterfaceIndex 17 -IPAddress 192.168.0.1 -PrefixLength 24 -DefaultGateway 192.168.0.254

We would like to be able to set the static IP address on any physical wired adapter. We can get the physical adapters like this:

Get-NetAdapter –Physical

That will return something like this:

Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed
----                      --------------------                    ------- ------       ----------             ---------
Ethernet                  Intel(R) Ethernet Connection (5)        17      Disconnected 00-11-22-33-44-55      1 Gbps
Wi-Fi                     Intel(R) Wireless LAN                   7       Disconnected 00-11-22-33-44-56      72 Mbps
  1. Is there any way to get only the wired adapters and not the wireless adapters, other than by name (which could be anything)?
  2. Since there may be multiple wired adapters (however rare), and since we don't know which physical adapter might be used, we would like to set the static IP address to each wired adapter. What can we do (something like a "for/each" loop) to set the IP address on each wired adapter on the PC?

EDIT: In reading some more , it seems that since Windows 8 that the wired adapter may be configured to be statically-named "Ethernet". Since we're only targeting Windows 10, can we really just add a query to get only adapters whose name is like ' Ethernet% ?

你尝试过类似的东西吗

Get-NetAdapter -Physical | Where-Object { $_.Name -match "^Ethernet" }

I would use

Get-NetAdapter -Physical | Where-Object { $_.PhysicalMediaType -eq "802.3" }

This seems to be a good indicator of physical ethernet only adapters, and it will get adapters that don't have "Ethernet" in the Description/DisplayName. WLAN devices will have Native 802.11 (or otherwise indicative of a Wireless Adapter).

Do Get-NetAdapter | Export-CSV C:\\Path\\To\\CSV Get-NetAdapter | Export-CSV C:\\Path\\To\\CSV to get all properties for all adapters on the system. You may find more properties to leverage in your filtering.

As for setting the IP Address on all ethernet adapters, this should work fine (although I caution that setting the same IP Address on multiple adapters may cause trouble in the however unlikely event that multiple ethernet adapters are in use simultaneously):

$ethernetAdapters = @(Get-NetAdapter -Physical | Where-Object {$_.PhysicalMediaType -eq "802.3"})
$staticIPAddress = "0.0.0.0"
$gateway = "0.0.0.0"
$prefixLength = "24"

foreach ($adapter in $ethernetAdapters) {
    New-NetIPAddress -IPAddress $staticIPAddress -AddressFamily IPV4 -DefaultGateway $gateway -PrefixLength $prefixLength -InterfaceIndex $adapter.InterfaceIndex
}

Remember to replace the variables with information for your environment, and specifically the IP address you'll use for the adapters.

You may wish to figure a way to generate a new IP Address for each $adapter , maybe in succession (*.200, *.201 etc.), with a portion that calls out to a centralized file with ip addresses, mac addresses and serial numbers for record keeping.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM