2017-10-16 20 views
0

DHCP 장치에 대한 (나중에 예약 추가) 스크립트를 작성하려고합니다. 문제는 하나의 범위가 있다는 것입니다.이 범위 내에서 특정 유형의 장치를 추가해야하는 다른 IP 범위로 수동으로 분할했습니다.Windows, DHCP 서버 예약 - 무료 IP 주소 찾기

예. 범위 10.92.0.0/24는 그리고 우리는 등 안드로이드 폰 등 내가 어디에 스크립트가 할 수있는 지점을 가지고

에 대한 10.92.0.10.50 아이폰에 대한

10.92.0.10-20로 범위를 지정 내가 제공하는 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++ 
} 

답변

-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

도움말에서 사용 가능한 임대 및 예약 목록을 검토한다고 말하면서 예약 만합니다. –