I ran into a situation where users going between different docks needed to connect to a directly connected network PC attached to the dock. The problem was every time they moved to a new dock the ethernet adapter was obviously different and needed IT to set the static IP. In my scenario their primary internet connection is wireless and the ethernet connection is just to talk to this other PC. (Yes, a strange setup)
I needed to come up with a script that would find the local active ethernet and set the IP, but only for the active Ethernet interface. Took some doing, but finally got the query to output the InterfaceIndex that was in a valid format.
IP's given below as examples
The pattern is a regex that looks for numbers 0-99 to get the InterfaceIndex number
#set variables.
$ip = "192.168.1.100"
$prefix = "24"
$GW = "192.168.1.1"
$adapter = Get-NetAdapter | where {($_.Name -like "Ethernet*") -and ($_.Status -eq 'Up')} | select ifIndex | out-string -stream | select-string -pattern "\b([0-9]|[1-9][0-9])\b"
$index = $adapter -replace(' ','')
#Set static IP based on result
New-NetIPAddress -IPAddress $ip -DefaultGateway $GW -PrefixLength $prefix -InterfaceIndex $index