【问题标题】:Windows, DHCP Server Reservation - finding free IP addressWindows, DHCP 服务器保留 - 查找免费 IP 地址
【发布时间】:2017-10-16 15:07:50
【问题描述】:

我正在尝试编写一个脚本,它会为 DHCP 设备找到(并稍后添加预留)。我遇到的问题是有一个范围,在这个范围内,我们手动划分了不同的 IP 范围,应该添加某种类型的设备。

例如范围 10.92.0.0/24 我们将范围分配为

10.92.0.10-20 适用于 iPhone 等。 10.92.0.10.50 适用于 Android 手机等

我知道脚本可以通过我提供给它的 IP 范围,并获取 DHCP 保留或显示错误。我一直在想第一个Error可以作为免费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++
}

【问题讨论】:

    标签: windows powershell server dhcp


    【解决方案1】:

    为什么不使用专用的 cmdlet Get-DhcpServerv4FreeIPAddress

    Get-DhcpServerv4FreeIPAddress -ScopeId "192.168.1.0" -StartAddress "192.168.1.100" -EndAddress "192.168.1.140" -ComputerName "dhcpserver.company.com"
    

    【讨论】:

    • 帮助它说它通过了可用租约和预订列表,我只关注预订。
    【解决方案2】:

    您可以将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++
    }
    

    【讨论】:

    • 有一个用于获取免费 DHCP 服务器租约的专用 cmdlet:Get-DhcpServerv4FreeIPAddress
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 2014-07-03
    • 2014-04-17
    • 2020-11-01
    • 1970-01-01
    • 2015-03-23
    相关资源
    最近更新 更多