2017-10-16 142 views
0

我试着写了一个脚本,会发现(并在以后添加预订)的DHCP设备。我遇到的问题有一个范围,在这个范围内,我们手动划分了不同的IP范围,应该添加某些类型的设备。的Windows,DHCP服务器保留 - 寻找免费的IP地址

E.g.范围10.92.0.0/24和我们指定范围为

10.92.0.10-20的iPhone等 10.92.0.10.50 Android手机等

我已经得到的地步脚本通过我提供给它的IP范围,以及GET GET保留或显示错误。我一直在想,第一个错误可以被视为免费的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++ 
} 

回答

-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++ 
} 
+0

詹姆斯它的工作,谢谢! –

+0

没有为获得免费的DHCP服务器租用一个专用的cmdlet:'GET-DhcpServerv4FreeIPAddress'。 – Adamar

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" 
+0

在帮助它说,它通过提供租赁和保留的名单,我只保留后我。 –