可以在批处理文件或其他脚本中使用用于动态主机配置协议 (DHCP) 的 Netsh 命令自动执行任务。下面的范例批处理文件说明了如何使用 DHCP 的 Netsh 命令执行多种相关任务。
在该范例过程的情形中,DHCP-01 是 IP 地址为 192.168.0.2 的 DHCP 服务器。该过程将新的作用域添加到名为 MyScope、IP 地址为 192.168.10.0、子网掩码为 255.255.255.0 以及注释为 NewScope 的 DHCP-01。然后配置作用域的地址范围(从192.168.10.1 到 192.168.10.254)、排除范围(从192.168.10.1 到 192.168.10.25 )以及路由器 IP 地址(DHCP 选项 003)。然后将作用域设置为活动状态。
详细信息,请参阅设置作用域、设置选项和 DHCP 选项。
有关用于 DHCP 的 Netsh 命令的详细信息和完整列表,请参阅用于 DHCP 的 Netsh 命令。
在下面的范例过程中,注释行前面以“rem”为标记。Netsh 将忽略注释。
rem one DHCP server:
rem (DHCP-01) 192.168.0.2
rem 1. Connect to (DHCP-01), and add the scope MyScope with IP address 192.168.10.0,
rem 1.1 subnet mask 255.255.255.0, and the comment NewScope.
netsh dhcp server 192.168.0.2 add scope 192.168.10.0 255.255.255.0 MyScope NewScope
rem 2. Connect to (DHCP-01 MyScope), and add IP address range 192.168.10.1 to 192.168.10.254 for distribution
rem 2.1 and the default ClientType of DHCP.
netsh dhcp server 192.168.0.2 scope 192.168.10.0 add iprange 192.168.10.1 192.168.10.254
rem 3. Connect to (DHCP-01 MyScope), and add IP exclusion range 192.168.10.1 to 192.168.10.25
rem 3.1 and the default ClientType of DHCP.
netsh dhcp server 192.168.0.2 scope 192.168.10.0 add excluderange 192.168.10.1 192.168.10.25
rem 4. Connect to (DHCP-01 MyScope), and set the value of option code 003
rem 4.1 to list two router IP addresses (10.1.1.1, 10.1.1.2).
netsh dhcp server 192.168.0.2 scope 192.168.10.0 set optionvalue 003 IPADDRESS 10.1.1.1 10.1.1.2
rem 5. Connect to (DHCP-01 MyScope), and set the scope state to active.
netsh dhcp server 192.168.0.2 scope 192.168.10.0 set state 1
rem 6. End example batch file.
下表列出了该范例过程中使用的 netsh dhcp 命令。