2017-10-16 155 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

在幫助它說,它通過提供租賃和保留的名單,我只保留後我。 –