DHCP PowerShell Export Import cmdlets in Windows Server 2012
DHCP PowerShell in Windows Server 2012, in addition to providing cmdlets for DHCP server management, provides cmdlets for exporting (Export-DhcpServer) and importing (Import-DhcpServer) DHCP Server configuration and IP address lease data. Unlike NetSH.exe export/import functionality which uses binary file format for the exported data, these cmdlets are designed for human readability as well as modification of the exported data and hence provide the exported data in an XML file.
Powershell import and export functionality provides the ability to migrate DHCP server configuration (optionally including lease data) from source server to a destination server. Here are some key administrative scenarios which are addressed by the export/import functionality:
- A DHCP server running on Windows Server 2008/Windows Server 2008 R2/Windows 2012 Server is being migrated to a DHCP server running on Windows Server 2012. The migration of server requires migration of configuration of the server and the scopes as well as migration of IP address lease data from the source to the destination server.
- As part of an exercise to optimally utilize the existing hardware, scopes running on 2 DHCP servers need to be migrated to a newly acquired hardware. The new hardware is capable of handling the load which is currently handled by the 2 DHCP servers. The admin exports the scopes configured on each of the current servers and migrates the same to the new destination server.
- Similar to scenario #1 above, a DHCP server is being migrated to a new server. However, in contrast to #1 above, the admin only cares to move the configuration information from the older server. The migration of lease information is beyond the purview of this migration project.
- In a migration/consolidation project, some of the option values such as DNS server, DNS domain and default gateway information needs to be changed to new values as the DNS server, domain etc is undergoing a change in the migration. Administrator will export the settings from source server, modify the XML file and then import it to the new server.
- Network infrastructure is being setup at a new satellite office. The network settings at the new office remain similar to the one in the head office. Hence, admin wishes to reuse the configuration from head office, make the needed adjustments e.g. change of ScopeID/IP Address ranges, default gateway and import this configuration on the DHCP server at the new satellite office.
- DHCP administrator has a DHCP server running on Windows Server 2008 or Windows Server 2008 R2 which he wants to migrate to DHCP failover deployment with 2 DHCP servers running Windows Server 2012. The current DHCP server has server level configuration – option values, option and class definitions (besides those which come built in). In this scenario, he will migrate the current DHCP Server including scope/Lease information to one of the Windows Server 2012 DHCP servers (primary DHCP server). Only server level configuration (using -ServerConfigOnly parameter) will be migrated to second Windows Server 2012 DHCP server. After these steps, failover can be configured from the primary DHCP server to the secondary DHCP server. See this blog post for the set of steps to achieve this migration.
- An admin is migrating his current DHCP server deployment from a 3rd party DHCP server to Windows DHCP server. He writes a script to convert the 3rd party DHCP configuration file to the XML file format required by Import-DhcpServer cmdlet. He then uses the converted XML file with Import-DhcpServer to import the configuration to Windows Server 2012 DHCP server to complete the migration.
DHCP Powershell Export/Import works on published XML schema which is part of the DHCP Powershell module installation and can be found at $PSHOMEModulesDhcpServer location.
Export-DhcpServer [-File] <String> [-ScopeId <IPAddress>] [-Prefix <IPAddress>] [-Force] [-Leases] [-ComputerName <String>] [-CimSession <CimSession>] [<CommonParameters>]
The Export-DhcpServer cmlet exports DHCP server and lease configuration information to an XML file specified by ‘File’ parameter. The scopeId (IPv4) and prefix (IPv6) parameters allow the admin to export only the specified IPv4 or IPv6 scopes respectively and thus provide a mechanism to selectively migrate specific scopes. Along with the specified scopes, the export cmdlet will also export server level configuration i.e. server level option values, option and class definitions, MAC address filters. This ensures that the effective configuration of the specified scopes on the destination server when this file is imported is same as the destination server.
If neither ScopeId nor Prefix is specified, the entire server configuration including all scopes (v4 and v6) is exported by the cmdlet. If Leases switch is specified, the lease data is also exported in addition to configuration data. Since the lease data can get voluminous, running Export-DhcpServer with –Leases is expected to take considerably longer time than exporting just configuration data.
If ComputerName or CimSession parameter is given, configuration from specified remote DHCP server is exported. If XML file to be exported already exists, cmdlet will throw an error. You can use the Force parameter to overwrite already existing XML file.
Export is supported from Windows Server 2008, Windows Server 2008 R2 and Windows Server 2012.
Import-DhcpServer [-File] <string> [-BackupPath] <string> [-ScopeId <ipaddress>] [-Prefix <ipaddress>] [-ScopeOverwrite] [-Leases] [-ServerConfigOnly] [-Force] [-ComputerName <string>] [-CimSession <CimSession>] [-WhatIf] [-Confirm] [<CommonParameters>]
The Import-DhcpServer cmdlet imports the DHCP server configuration (and optionally lease data) from the file specified by ‘File’ parameter. Just as in the case of Export-DhcpServer, using ScopeId (IPv4) and/or Prefix (IPv6) parameters, the admin can selectively import the specified scopes/prefixes. As noted under Export-DhcpServer, the server level configuration is imported as well when scopes/prefixes are specified for reasons mentioned earlier.
If neither ScopeId nor Prefix is specified, all the configuration contained in the file (and optionally lease data) will be imported by the cmdlet. If Leases switch is specified, the lease data in specified file is also imported in addition to configuration data. Import operation with leases is expected to take longer depending on the number of leases being imported.
If ServerConfigOnly switch is specified, only server level configuration will be imported on the destination server. If ComputerName or CimSession parameter is given, configuration from specified remote DHCP server is exported.
Before starting the actual import operation, the cmdlet does a backup of the DHCP Server to the path as specified by the ‘BackupPath’ parameter. This gives administrator a way to restore to the original settings in case there is a terminating error during the import operation.
The –ScopeOverWrite allows the admin to continue with import even if the scope is already present on the destination DHCP server. If this parameter is not specified, the import cmdlet gives a non-terminating error for a scope which is already present on the DHCP server and continues to process the next scope in the file.
The cmdlet seeks a user confirmation which can be overridden using the –Force parameter.
Import is supported on DHCP server running on Windows Server 2012.
Here is the comparison table comparing the PowerShell Export/Import cmdlets and the older netsh export/import -
Uses an XML file format for export/import
Uses a binary file format for export/import
Easy to modify/update or even create a new XML file for import/export since XML schema is published.
Not possible to read, update or create the binary file used for import/export.
Can export from or import to a remote DHCP Server
Works only on local DHCP Server
The DHCP Server service can continue uninterrupted during exporting or importing DHCP Server
DHCP Server service is stopped while exporting from or importing to the DHCP server
Gives flexibility of opting if lease data should be exported/imported
Lease data is always exported/imported.
Time taken by PowerShell Export/Import is longer than NetSH Export/Import as PowerShell export/import uses the RPC transport to read/write data to the DHCP server.
Below are some examples to elucidate the usage of Powershell Export, Import cmdlets:
1. Export-DhcpServer -File C:dhcpexport.xml -ComputerName dhcpserver.contoso.com
The above command will export all the DHCP server (DHCPv4 and DHCPv6 configuration including all the IPv4 and IPv6 scopes present on the ‘dhcpserver.contoso.com’ server to ‘dhcpexport.xml’ file
2. Export-DhcpServer -File C:exportdirdhcpexport.xml -ScopeId 10.10.10.0,22.214.171.124 -ComputerName dhcpserver.contoso.com
The above command will export scopes ‘10.10.10.0’ and ‘126.96.36.199’ present on the ‘dhcpserver.contoso.com’ server to the export file ‘dhcpexport.xml’. The DHCPv4 server level configuration will also be exported to the specified file.
3. Export-DhcpServer .dhcpexport.xml –Prefix 3ffe::1 -Leases
The above command will export the IPv6 prefix ‘3ffe::1’ from the local DHCP Server to the XML file ‘dhcpexport.xml’ in the directory from where the cmdlet is run including all the leases in the IPv6 scope. The DHCPv4 server level configuration will also be exported to the specified file. Note that since File parameter is positional, the parameter name is not explicitly given.
4. Import-Csv ScopeList.txt | Export-DhcpServer -File C:exportdirdhcpexport.xml -Leases
The above command exports a list of scopes specified in ScopeList.txt to the specified export file in XML format. The above command works by pipelining the objects containing the ScopeIds outputted by Import-Csv to the second cmdlet Export-DhcpServer which exports the specified scopes.
The ScopeList.txt file should contain the following format:
5. Import-DhcpServer -ComputerName dhcpserver.contoso.com -File C:exportsdhcpexport.xml -BackupFilePath C:dhcpbackup
The above command imports the configuration data from file ‘dhcpexport.xml’ onto the server dhcpserver.contoso.com. All the configuration data contained in the xml file other than leases is imported. If an IPv4 or IPv6 scope is present in both the XML file and the target machine ‘dhcpserver.contoso.com’, such scopes are not migrated and a warning for the same notifying that the scope is not migrated is shown.
6. Import-DhcpServer -ComputerName dhcpserver.contoso.com -File C:exportsdhcpexport.xml -BackupFilePath C:dhcpbackup -leases
The above command imports the configuration data from file ‘dhcpexport.xml’ onto the server dhcpserver.contoso.com. All the configuration data contained in the xml file included lease data are imported.
7. Import-DhcpServer C:exportsdhcpexport.xml C:dhcpbackup -ScopeId 10.10.10.0 –Prefix 3ffe::1 –Leases -Force
The above command imports the configuration settings including lease data for scopes 10.10.10.0 and 3ffe::1 from the XML file ‘dhcpexport.xml’ onto the local DHCP server. If the export file contains other scopes besides 10.10.10.0 and 3ffe::1, the same are ignored. Server level configuration data for both IPv4 and IPv6 if present in the export file is also imported onto the server. Import cmdlet by default seeks for user confirmation. The ‘-Force’ parameter suppresses the default confirmation prompt.
8. Import-DhcpServer -ComputerName dhcpserver.contoso.com -File C:exportsdhcpexport.xml -BackupFilePath C:dhcpbackup -Leases -ScopeOverwrite
The above command imports the configuration and lease data for all the IPv4 and IPv6 scopes from the XML file ‘dhcpexport.xml’ onto the server dhcpserver.contoso.com. DHCPv4 server level configuration data if present in the export file is also imported onto the server. If the scopes present in the xml file are already present on the server dhcpserver.contoso.com, those are deleted and recreated from the data in the export file.
9. Import-DhcpServer -ComputerName dhcpserver.contoso.com -File C:exportsdhcpexport.xml -BackupFilePath C:dhcpbackup -ServerConfigOnly
The above command imports only the server level configuration data in the specified file onto the server dhcpserver.contoso.com. Any scope configuration data if present in the file is ignored.
$ScopeIdList = Get-Content ScopeList.txt
Import-DhcpServer -ComputerName dhcpserver.contoso.com -File C:exportdirdhcpexport.xml -Leases -ScopeId $ScopeIdList
The above set of commands imports a list of scopes specified in ScopeList.txt to the server dhcpserver.contoso.com. The first command Get-Content gets the list of scopes to be imported in the variable ScopeIdList. The same is then used as input parameter ScopeId of the second command to import the scopes.
The ScopeList.txt file should contain the following format:
Hope these new set of PowerShell tools are useful to the Windows DHCP administrator!
As always, we are keen to hear your feedback!