我试图编写一个可以找到(后来添加预留)DHCP设备的脚本。问题是我有一个范围,在此范围内我们手动划分为不同的IP范围,其中应添加某些类型的设备。
E.g。范围10.92.0.0/24,我们将范围指定为
10.92.0.10-20适用于iPhone等 10.92.0.10.50为Android手机等。
我已经明白了脚本可以通过我提供给它的IP范围,以及GET 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++
}
答案 0 :(得分:0)
为什么不使用专用cmdlet Get-DhcpServerv4FreeIPAddress
?
Get-DhcpServerv4FreeIPAddress -ScopeId "192.168.1.0" -StartAddress "192.168.1.100" -EndAddress "192.168.1.140" -ComputerName "dhcpserver.company.com"
答案 1 :(得分:-1)
您可以将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++
}