我正在尝试编写一个脚本,它将为DHCP设备找到(稍后添加预约)。我遇到的问题是,有一个范围,在这个范围内,我们手动划分为不同的IP范围,其中应该添加某些类型的设备。
例如范围10.92.0.0/24,我们将范围指定为
10.92.0.10-20用于iPhones等,10.92.0.10.50用于安卓手机等。
我已经知道脚本可以遍历我提供给它的IP范围,或者获得DHCP预订或者显示错误。我一直在想,第一个错误可以被视为免费IP。
有人有什么想法吗?)
# Get DHCP Scope
$Start = 100
$End = 140
$DHCPServer = "dhcpserver.company.com"
# Find Free IP address
#It can use to get DHCP reservation by IP and find the one which returns error - which can be used as the free one - loop done
#Now how to tell it to stop when the error occures?
While ($Start -le $End) {
$IP = "10.92.0.$Start"
Write-Host "Reservation for: $IP" -ForegroundColor Cyan
Get-DhcpServerv4Reservation -ComputerName $DHCPServer -IPAddress $IP
$Start++
}发布于 2017-10-16 15:32:44
您可以将Get-DhcpServerv4Reservation的输出分配给一个变量,然后执行以下操作:
While ($Start -le $End) {
$IP = "10.92.0.$Start"
$reservation = Get-DhcpServerv4Reservation -ComputerName $DHCPServer -IPAddress $IP -ErrorAction SilentlyContinue
if($reservation){
Write-Host "Reservation for: $IP" -ForegroundColor Cyan
$reservation
}
else {
Write-Host "No reservation found for: $IP" -ForegroundColor Red
break #comment out to continue through all IPs in Scope
}
$Start++
}发布于 2017-10-16 21:36:54
为什么不使用专用cmdlet Get-DhcpServerv4FreeIPAddress
Get-DhcpServerv4FreeIPAddress -ScopeId "192.168.1.0" -StartAddress "192.168.1.100" -EndAddress "192.168.1.140" -ComputerName "dhcpserver.company.com"https://stackoverflow.com/questions/46773452
复制相似问题