Now, how can I modify the values for DestinationPrefix and NextHop on those two routes without deleting and adding them from scratch and using the Set-NetRoute command?
I’d get the route with Get-NetRoute along with the proper parameters and pipe it to Set-NetRoute with the desired settings.
BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
Now, it’s hard for me to find the proper way to set the Set-NetRoute command for the routes I want to modify. I need to modify the netmask and the gateway of some routes in particular. Is there a way to do so without deleting the routes already created?
Well, the criteria is quite simple. In my organization I have different routes created for different networks.
My goal here is to create a script that can allow me to manage and edit those routes just replacing the netmask and the gateway so I can point them as I desire when I have to test migrations of the environment (with network change).
BTW, the * . * . * . * / * is in reality an actual IP address but I put it with * to resume the example.
In this case it is misleading to replace them with wildcards. You might have replaced them with some generic ones.
So how many result do you get when you run a query with the particular IP address? If it is only one you can omit the Select-Object and add your Set-NetRoute instead.
Well, to be honest with you here is the part I will need some guidance because I’m not so sure how I can use the Set-NetRoute command properly.
I read some documentation but it’s a little bit tricky for me because I’m not used to write code.
As I can get, the parameters I need to change for example for the route “1.0.0.0” is the DestinationPrefix for changing the netmask and the NextHop for the gateway.
Am I right? If that’s so, how can I “set” the new netmask and gateway?
Let’s say I want to change the route 1.0.0.0/0 with gateway 192.168.1.1 to 1.0.0.0/24 with gateway 192.168.1.2.
I urgently recommend for you to do a step back and start with learning the very basics of PowerShell first. This will save you from a lot of wasted time and frustrations.
I tried different ways but I’m always getting the same error that tells me the object is not found when the compilation tries to run the Set-NetRoute. Here I send you an example:
PS C:\windows\system32> Get-NetRoute -PolicyStore PersistentStore -DestinationPrefix 192.168.2.0/24 -NextHop 192.168.2.1
ifIndex DestinationPrefix NextHop RouteMetric ifMetric PolicyStore
------- ----------------- ------- ----------- -------- -----------
0 192.168.2.0/24 192.168.2.1 1 Persiste...
PS C:\windows\system32> Get-NetRoute -PolicyStore PersistentStore -DestinationPrefix 192.168.2.0/24 -NextHop 192.168.2.1 |
>> Set-NetRoute -PolicyStore PersistentStore -DestinationPrefix 192.168.2.0/22 -NextHop 192.168.2.2
Set-NetRoute : Consulta CIM para instancias de la clase ROOT/StandardCimv2/MSFT_NetRoute en el servidor CIM : SELECT * FROM MSFT_NetRoute WHERE ((DestinationPrefix LIKE
'192.168.2.0/22')) AND ((InterfaceIndex = 0)) AND ((InterfaceAlias LIKE 'iftype0[_]0')) AND ((NextHop LIKE '192.168.2.2')) no encontró objetos MSFT_NetRoute que coincidan. Compruebe los
parámetros de consulta e inténtelo de nuevo.
En línea: 2 Carácter: 1
+ Set-NetRoute -PolicyStore PersistentStore -DestinationPrefix 192.168. ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (MSFT_NetRoute:String) [Set-NetRoute], CimJobException
+ FullyQualifiedErrorId : CmdletizationQuery_NotFound,Set-NetRoute
I don’t get why I cannot change the record when I know that it can find it properly.
Hmmm … I am by no means a network guy and I don’t have any experience with such a task but it looks like you cannot change a route the way you want. So you are doomed to either delete the existing one and create a new one with the desired settings or simply create a new one next to the existing one. Sorry.
You’re right about that but, on this particular case, it’s a little bit tricky because if you have 10 routes to change pointing to different networks, then you have to collect all the info first and then perform the changes for each record. It would be more practical at that point to do it manually by CMD.