Add static route from text file

Welcome Forums General PowerShell Q&A Add static route from text file

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
11 months, 3 weeks ago.

  • Author
    Posts
  • #89839

    Participant
    Points: 1
    Rank: Member

    Hi

    I need to transfer static routes from server to server .
    If I get the old server routes by
    Get-NetRoute |where {$_.RouteMetric -eq '1'} | FT DestinationPrefix,NextHop,RouteMetric | out-file c:\newroutes.txt

    is this possible to add them to the new server by using New-NetRoute from the text file ?

    or course any other way to make this migration is welcome .

    Thanks

  • #89842

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    First, if you're looking to transfer data, text is a terrible format. Don't use Format-Table – it discards there structure of the data. Just Export-CliXML. That maintains the structure of the data – the properties of the objects. You can Import-CliXML them to recreate the objects.

    Text is lovely on Linux. Well, not really, but try to avoid text files as a means of data storage.

    https://technet.microsoft.com/en-us/library/hh826148(v=wps.630).aspx describes which parameters New-NetRoute can accept from the pipeline (e.g., "Import-CliXML mydata.xml | New-NetRoute"). If you need it to use properties which don't accept pipeline input, then you'll have to ForEach it.

    Import-CliXML whatever.xml |
    ForEach-Object {
    New-NetRoute -Parameter $_.Property -OtherParam $_.OtherProp
    }

    Within ForEach, $_ lets you access one object at a time from your CliXML file, so you can refer to the individual properties like RouteMetric and such.

    • #89995

      Participant
      Points: 1
      Rank: Member

      Thanks Don

      On source server :
      Get-NetRoute |where {$_.RouteMetric -eq '1'} | export-CliXML c:\newroutes.xml

      On Destination server :
      Import-CliXML c:\newroutes.xml |
      ForEach-Object {
      New-NetRoute -DestinationPrefix $_.DestinationPrefix -NextHop $_.NextHop -RouteMetric $_.RouteMetric -InterfaceAlias $_.InterfaceAlias
      }

      Did the job

The topic ‘Add static route from text file’ is closed to new replies.