export network settings and import after reboot

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

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.

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

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.

[quote quote=228367]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.

[/quote]

Thanks, looks better now with xml files.

Patrick

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:

new-netroute : Invalid parameter NextHop MSFT_NetRoute (InstanceID = ":8:8:8:9:55;A55;A?8?:8@8???55;")
At line:16 char:9
+ new-netroute -InterfaceAlias $NetIPConfigurationsAlias -NextHop $NetIPCo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (MSFT_NetRoute:ROOT/StandardCimv2/MSFT_NetRoute) [New-NetRoute], CimException
+ FullyQualifiedErrorId : Windows System Error 87,New-NetRoute
variable:
PS C:\Windows\system32> $NetIPConfigurationsIPv4DefGW

ifIndex DestinationPrefix NextHop RouteMetric PolicyStore PSComputerName 
------- ----------------- ------- ----------- ----------- -------------- 
15 0.0.0.0/0 10.100.0.254 256 ActiveStore 

script section:

$NetIPConfiguration = Import-Clixml $exportpath\NetDNSGW.xml

#### sets variable and change it if wrong
ForEach ($NetIPConfigurations in $NetIPConfiguration){
$NetIPConfigurationsAlias = $($NetIPConfigurations.InterfaceAlias)
$NetIPConfigurationsIPv4DefGW = $($NetIPConfigurations.IPv4DefaultGateway)
$NetIPConfigurationsDNSServer = $($NetIPConfigurations.DNSServer)
Write-host -ForegroundColor yellow "set DNS and Gateway for...
"
write-host $NetIPConfigurationsAlias
####
#### query for empty GW and skip, only set GW if present before otherwise 0.0.0.0 is set
####
if($NetIPConfigurationsIPv4DefGW)
{
new-netroute -InterfaceAlias $NetIPConfigurationsAlias -NextHop $NetIPConfigurationsIPv4DefGW -DestinationPrefix 0.0.0.0/0 -Verbose
}
ELSE
{
write-host -ForegroundColor yellow "no GW set in old config skipping... "
}

Thanks

Patrick

If I got it right you should use instead of

$NetIPConfigurationsIPv4DefGW = $($NetIPConfigurations.IPv4DefaultGateway)
rather
$NetIPConfigurationsIPv4DefGW = $($NetIPConfigurations.IPv4DefaultGateway.NextHop)

[quote quote=231796]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
[/quote] yea that did the trick!

Thanks
Patrick

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”:

***************************** IMPORT old IP config - Interface name must be the same as before *******************************

set NetadapterIP and subnet for:
Storage 172.19.6.19 21
VERBOSE: Performing operation "New" on Target "NetIPAddress -IPv4Address 172.19.6.19 -InterfaceIndex 16 -Store Active"
VERBOSE: Performing operation "New" on Target "NetIPAddress -IPv4Address 172.19.6.19 -InterfaceIndex 16 -Store
Persistent"

set NetadapterIP and subnet for:
LAN 10.100.6.19 21
VERBOSE: Performing operation "New" on Target "NetIPAddress -IPv4Address 10.100.6.19 -InterfaceIndex 4 -Store Active"
VERBOSE: Performing operation "New" on Target "NetIPAddress -IPv4Address 10.100.6.19 -InterfaceIndex 4 -Store
Persistent"

***************************** IMPORT old Gateway and DNS config *******************************

set DNS and Gateway for:
Storage
no GW set in old config skipping...

no DNS set in old config skipping...

set DNS and Gateway for:
LAN
VERBOSE: Performing operation "New" on Target "NetRoute -DestinationPrefix 0.0.0.0/0 -InterfaceIndex 4 -NextHop
10.100.0.254 -Store Active"
VERBOSE: Performing operation "New" on Target "NetRoute -DestinationPrefix 0.0.0.0/0 -InterfaceIndex 4 -NextHop
10.100.0.254 -Store Persistent"
VERBOSE: Microsoft DNS Client settings will be changed as requested.
This will affect name resolutions on the adapter "LAN".
VERBOSE: Microsoft DNS Client settings will be changed as requested.
This will affect name resolutions on the adapter "LAN".

Windows IP Configuration

Registration of the DNS resource records for all adapters of this computer has been initiated. Any errors will be reported in the Event Viewer in 15 minutes.

!!all done check if config is ok!!

=> should be finished here but starts again?


IPAddress : 172.19.6.19
InterfaceIndex : 16
InterfaceAlias : Storage
AddressFamily : IPv4
Type : Unicast
PrefixLength : 21
PrefixOrigin : Manual
SuffixOrigin : Manual
AddressState : Tentative
ValidLifetime : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource : False
PolicyStore : ActiveStore

IPAddress : 172.19.6.19
InterfaceIndex : 16
InterfaceAlias : Storage
AddressFamily : IPv4
Type : Unicast
PrefixLength : 21
PrefixOrigin : Manual
SuffixOrigin : Manual
AddressState : Invalid
ValidLifetime : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource : False
PolicyStore : PersistentStore

IPAddress : 10.100.6.19
InterfaceIndex : 4
InterfaceAlias : LAN
AddressFamily : IPv4
Type : Unicast
PrefixLength : 21
PrefixOrigin : Manual
SuffixOrigin : Manual
AddressState : Tentative
ValidLifetime : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource : False
PolicyStore : ActiveStore

IPAddress : 10.100.6.19
InterfaceIndex : 4
InterfaceAlias : LAN
AddressFamily : IPv4
Type : Unicast
PrefixLength : 21
PrefixOrigin : Manual
SuffixOrigin : Manual
AddressState : Invalid
ValidLifetime : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource : False
PolicyStore : PersistentStore


Caption :
Description :
ElementName :
InstanceID : :8:8:8:9:55>55;:8;::8:8<?>55;
AdminDistance :
DestinationAddress :
IsStatic :
RouteMetric : 256
TypeOfRoute : 3
AddressFamily : IPv4
CompartmentId : 1
DestinationPrefix : 0.0.0.0/0
InterfaceAlias : LAN
InterfaceIndex : 4
InterfaceMetric : 50
NextHop : 10.100.0.254
PreferredLifetime : 10675199.02:48:05.4775807
Protocol : NetMgmt
Publish : No
State : Alive
Store : ActiveStore
ValidLifetime : 10675199.02:48:05.4775807
PSComputerName :
ifIndex : 4


Caption :
Description :
ElementName :
InstanceID : :8:8:8:9:55>55;:8;::8:8<?>55:
AdminDistance :
DestinationAddress :
IsStatic :
RouteMetric : 256
TypeOfRoute : 3
AddressFamily : IPv4
CompartmentId : 0
DestinationPrefix : 0.0.0.0/0
InterfaceAlias : LAN
InterfaceIndex : 4
InterfaceMetric : 50
NextHop : 10.100.0.254
PreferredLifetime : 10675199.02:48:05.4775807
Protocol : NetMgmt
Publish : No
State : Alive
Store : PersistentStore
ValidLifetime : 10675199.02:48:05.4775807
PSComputerName :
ifIndex : 4


InterfaceAlias InterfaceDescription IPv4Address IPv4DefaultGateway
-------------- -------------------- ----------- ------------------
Storage XenServer PV Network Device #1 {172.19.6.19}
LAN XenServer PV Network Device #0 {10.100.6.19} {MSFT_NetRoute (InstanceID = ":8:8:8:9:55?55;:8;::8:8???...


Press Enter to continue:

Thanks
Patrick

 

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.

[quote quote=231874]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.

[/quote]

Ok thanks will have a look.