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."
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.
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?
$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... "
}
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:
[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.