一、必要条件
1.winserver 2019
2.通外网,需要下载模版
3.一个名称叫dhcp_ip_list.xlsx的文件
1.附件为例子,修改其中的数据即可
4.默认租期为8小时
二、代码
Install-Module -Name ImportExcel
# Read the xlsx file
$data = Import-Excel -Path "./dhcp_ip_list.xlsx"
$leaseDuration = New-TimeSpan -Hours 8
# Iterate through each row
$data | ForEach-Object {
$row = $_
# Get the column values
$scope_name = $row."scope_name"
$start_ip = $row."start_ip"
$end_ip = $row."end_ip"
$mask = $row."mask"
$exclude_start_ip = $row."exclude_start_ip"
$exclude_end_ip = $row."exclude_end_ip"
$router = $row."router"
$scope_id = $row."scope_id"
$dns1 = $row."dns1"
$dns2 = $row."dns2"
$dns3 = $row."dns3"
# Create a new DHCP scope
Add-DhcpServerv4Scope -Name $scope_name -StartRange $start_ip -EndRange $end_ip -SubnetMask $mask -LeaseDuration $leaseDuration -State Active
# Set the router and DNS server
Set-DhcpServerv4OptionValue -ScopeId $scope_id -DnsServer $dns1,$dns2,$dns3 -Router $router
# Exclude the IP range
Add-DhcpServerv4ExclusionRange -ScopeId $scope_id -StartRange $exclude_start_ip -EndRange $exclude_end_ip
}