export network settings and import after reboot

Welcome Forums General PowerShell Q&A export network settings and import after reboot

Viewing 10 reply threads
  • Author
    Posts
    • #227776
      Participant
      Topics: 1
      Replies: 6
      Points: 26
      Rank: Member

      Hi

       

      Looking for a script to export all network settings (IP, GW, binding config) for all active adapters to a text file.

      Then import this text file and restore the settings in case the Server looses the config (Windows Server 2012 and above).

      anyone did this before? netsh is not helping.

      Something like this?

      Get-NetAdapterBinding | Export-csv $exportpath\NetBin.csv
      get-netipinterface -AddressFamily IPv4 | where {$_.InterfaceAlias -notlike “Loopback*”} | export-csv $exportpath\NetIP.csv

      $NetAdapterBinding = Import-CSV $exportpath\NetBin.csv
      Set-NetAdapterBinding -InputObject $NetAdapterBinding

      Thanks
      Patrick

    • #227803
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      Patrick, welcome to Powershell.org. Please take a moment and read the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!.

      When you post code, error messages, sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

      This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.

      We expect you to make an own attempt to get your task done or to solve your problem. If you have done so already please document here what exactly you have done and show your code. Then we probably might be able to help you step further.

      Have you tried to search for a solution? You may find something adaptable in the PowershellGallery. Another option would be to use DSC for the server configuration.

    • #228352
      Participant
      Topics: 1
      Replies: 6
      Points: 26
      Rank: Member

      So i tried this with export / import values, however how do I convert the string “FALSE” to a boolen after importing from csv?

      script sample:

      Thanks
      Patrick

    • #228367
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      I assume you used CSV files to save your settings. Then you will have to turn the properties back from string to boolean after importing them from csv. You could use an if statement in a calculated property for example. Another option would be to use Export-Clixml instead of Export-Csv to preserve the types of the exported properties.

    • #228913
      Participant
      Topics: 1
      Replies: 6
      Points: 26
      Rank: Member

      I assume you used CSV files to save your settings. Then you will have to turn the properties back from string to boolean after importing them from csv. You could use an if statement in a calculated property for example. Another option would be to use Export-Clixml instead of Export-Csv to preserve the types of the exported properties.

      Thanks, looks better now with xml files.

      Patrick

    • #231790
      Participant
      Topics: 1
      Replies: 6
      Points: 26
      Rank: Member

      So I managed to export all settings now to xml files (IP, subnet, gateway, DNS Server) and import it back after reboot.

      However, to set the default gateway I use “new-netroute” and it looks like my import from the xml to the variable is somehow wrong. it works for IP and subnet.

      The variable looks like this, how to I convert it to only the nexthob value into the variable?

      error:

      script section:

      Thanks

      Patrick

    • #231796
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      If I got it right you should use instead of

      rather

    • #231805
      Participant
      Topics: 1
      Replies: 6
      Points: 26
      Rank: Member

      If I got it right you should use instead of

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      $NetIPConfigurationsIPv4DefGW = $($NetIPConfigurations.IPv4DefaultGateway)
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      rather

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      $NetIPConfigurationsIPv4DefGW = $($NetIPConfigurations.IPv4DefaultGateway.NextHop)
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      yea that did the trick!

      Thanks
      Patrick

    • #231865
      Participant
      Topics: 1
      Replies: 6
      Points: 26
      Rank: Member

      So it is not pretty but the script basically works…

      One thing I don’t get, there is some sort of a “loop”. If the adapters are already configured with IP etc it runs with some errors but completes and will not overwrite anything.

      But if the adapter config is missing it will be set from the exported files correctly, all good but at the end it somehow restarts somewhere but can’t find where. How do I debug step by step?

      script output if it “loops”:

      Thanks
      Patrick

       

    • #231874
      Participant
      Topics: 7
      Replies: 2458
      Points: 6,439
      Helping Hand
      Rank: Community MVP

      Sorry. It is far beyond the scope of any forum to debug complex scripts like this. Especially if it’s that badly formatted and structured like yours.

      I’d recommend to read The Unofficial PowerShell Best Practices and Style Guide first, re-design your script and debug it while re-designing it.

    • #231886
      Participant
      Topics: 1
      Replies: 6
      Points: 26
      Rank: Member

      Sorry. It is far beyond the scope of any forum to debug complex scripts like this. Especially if it’s that badly formatted and structured like yours.

      I’d recommend to read The Unofficial PowerShell Best Practices and Style Guide first, re-design your script and debug it while re-designing it.

      Ok thanks will have a look.

Viewing 10 reply threads
  • The topic ‘export network settings and import after reboot’ is closed to new replies.