export network settings and import after reboot

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

Viewing 4 reply threads
  • Author
    Posts
    • #227776
      Participant
      Topics: 1
      Replies: 2
      Points: 17
      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: 4
      Replies: 2231
      Points: 5,414
      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: 2
      Points: 17
      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?

      "PS C:\Windows\system32> Set-NetAdapterBinding -Name $NetBindingName -ComponentID $NetBindingCompID -Enabled $NetBindingEnabled
      Set-NetAdapterBinding : Cannot process argument transformation on parameter 'Enabled'. Cannot convert value "System.String" to type "System.Boolean". Boolean parameters accept only 
      Boolean values and numbers, such as $True, $False, 1 or 0."

      script sample:

      Get-NetAdapterBinding | select Name,ComponentID,Enabled | Export-csv $exportpath\NetBin.csv -NoTypeInformation
      $NetAdapterBinding = Import-Csv $exportpath\NetBin.csv
      
      ForEach ($NetBindings in $NetAdapterBinding){
      $NetBindingName = $($NetBindings.Name)
      $NetBindingCompID = $($NetBindings.ComponentID)
      $NetBindingEnabled = $($NetBindings.Enabled)
      write-host $NetBindingName $NetBindingCompID $NetBindingEnabled
      }
      Set-NetAdapterBinding -Name $NetBindingName -ComponentID $NetBindingCompID -Enabled $NetBindingEnabled
      }

      Thanks
      Patrick

    • #228367
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      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: 2
      Points: 17
      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

Viewing 4 reply threads
  • You must be logged in to reply to this topic.