powershell interfaceindex new-netroute

Powershell to check -interfaceindex number put it into a variable then use the variable to apply static routes with new-netroute.

i have been looking at get-netipinterface and can display the info i need in the ifindex column i am however unsure how to get it into a variable and then use that to apply the static route.

with something like this New-NetRoute -DestinationPrefix "10.0.0.0/24" -InterfaceIndex "variable" -NextHop 192.168.0.1

PS C:\Users\Adam> Get-NetIPInterface "work"


ifIndex InterfaceAlias                  AddressFamily NlMtu(Bytes) InterfaceMetric Dhcp     ConnectionState PolicyStore
------- --------------                  ------------- ------------ --------------- ----     --------------- -----------
47      Work                            IPv4                  1400              35 Disabled Connected       ActiveStore
Adam FlookSenior Technical Services EngineerAsked:
Who is Participating?
 
oBdAConnect With a Mentor Commented:
Like that, for example:
$IfAlias = 'Work'
If ($IfIndex = Get-NetIPInterface -InterfaceAlias $IfAlias -AddressFamily IPv4 | Select-Object -ExpandProperty ifIndex) {
	Write-Host "Network interface '$($IfAlias)' found at index $($IfIndex)."
	New-NetRoute -DestinationPrefix "10.0.0.0/24" -InterfaceIndex $IfIndex -NextHop 192.168.0.1
} Else {
	Write-Error "Network interface '$($IfAlias)' not found!"
}

Open in new window

0
 
Adam FlookSenior Technical Services EngineerAuthor Commented:
Hi exactly like that thanks
0
 
Adam FlookSenior Technical Services EngineerAuthor Commented:
works exactly as needed
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.