16 August 2013

DHCP - PowerShell basics and netsh equivalency

Windows Server 2012 brings a huge improvement for DHCP.  One of the best things is that all DHCP functionality can now be driven from PowerShell.  Netsh is still supported but has been deprecated.

If you are migrating from Windows Server 2008R2 to Windows Server 2012 you will most probably use a combination of these as you are moving along.

DHCP Export

Export the whole DHCP server's configuration, scopes and leases.

Export-DhcpServer -File c:\DHCP\PS-Full-Export.xml -Leases
Netsh dhcp server v4 export c:\DHCP\NetSH-Full-Export.txt all

Export a single scope

Export-DhcpServer -File c:\DHCP\PS-SingleScope-Export.xml -Leases -ScopeId
Netsh dhcp server v4 export c:\DHCP\NetSH-SingleScope-Export.txt

Export multiple names scopes

Export-DhcpServer -File c:\DHCP\PS-MultipleScopes-Export.xml -Leases -ScopeId,,
Netsh dhcp server v4 export c:\DHCP\NetSH-MultipleScopes-Export.txt,,

DHCP Import
One thing to keep in mind is that if you export a DHCP server or scope with netsh you have to import it with netsh since PowerShell and netsh produce two different file types.  PowerShell also has a mandatory backup before you can import anything new.

To export the whole DHCP server's configuration, scopes and leases.

Import-DhcpServer -File c:\DHCP\PS-Full-Export.xml -BackupPath C:\DHCP\ -Leases
Netsh dhcp server v4 import c:\DHCP\NetSH-Full-Export.txt all

PowerShell DNS cmdlts also allow you to selectively restore just the server config and or the leases.

Import-DhcpServer -File c:\DHCP\PS-Full-Export.xml -BackupPath C:\DHCP\ -ServerConfigOnly
Import-DhcpServer -File c:\DHCP\PS-Full-Export.xml -BackupPath C:\DHCP\ -ScopeId,, -Leases

Setting Options
Options can be set when the scopes are created but to change settings on existing scopes you can use the following:

This sets the lease time

Set-DhcpServerv4OptionValue -ScopeId -OptionId 51 -Value 3600
Netsh dhcp server scope set optionvalue 51 DWORD 3600

You can also used named Option in PowerShell


Set-DhcpServerv4OptionValue -DnsServer -WinsServer -DnsDomain domain.com -Router -Wpad http://proxy.domain.com/wpad.dat

When setting non-named Options you have to specify the value in the correct format. Here is an example of where the option data is in hex.

set-DhcpServerv4OptionValue -ScopeId -OptionId 43 -Value 0x3A,0x02,0x01,0x2D,0xFF

Batching netsh commands with PowerShell
This is useful if you are working with and Windows Server 2008 R2 machine.  One way to import and export or make chnages to a large number of scopes with NetSh is to generate a text file with one line per scope id.  The files typically look like this

A simply way to feed each line into netsh is with the following PowerShell command sequence.

Get-content C:\dhcp\list.txt | foreach-object { Netsh dhcp server v4 export c:\dhcp\$_ $_}

Get-content e:\dhcp\list.txt | foreach-object { Netsh dhcp server \\serverip scope $_  set optionvalue 51 DWORD 3600}

Keeping failover pairs in sync
Once you have Migrated on to Fail-over pairs it is important to remember that leases and reservation are synced automatically but scope changes and options are not.  You can use the following to sync the options and scopes back up.

Invoke-DhcpServerv4FailoverReplication -ComputerName DHCPserverName

Other handy commands
There are now stacks of different ways to get visibility on your DHCP environment.  The eamples below should givea good indication of what is now very easy to do.

To get a nicely formatted list of the scope in the shell use the following

Get-DhcpServerv4Scope | select scopeid, name | Format-Table -AutoSize

You can also filter by the scope, this looks for any scope name that contains the word WiFi

Get-DhcpServerv4Scope | WHERE {$_.name -match "WiFI"} 

To then also get the usage statistics use

Get-DhcpServerv4Scope | WHERE {$_.name -match "Centre"} |Get-DhcpServerv4ScopeStatistics

If you would like to keep the scope name and description matched you can simply run the following:

Get-DhcpServerv4Scope |Set-DhcpServerv4Scope -Description {$_.name}

No comments:

Post a Comment