question

Proxiuz-8366 avatar image
0 Votes"
Proxiuz-8366 asked IanXue-MSFT answered

Powershell script wont reserve IP addresses.

Hello!

This script is supposed to first tell the user if the free IPs of each scope is more or lease than the input number. Then its supposed to ask the user what scope and to which mac address the lease should reserve to. Even tho the script should work as intended the reservation does not exist in my DHCP.

 Write-Host 'Enter a number of addresses'
 $Input = Read-Host
 $inputInt = [int]$Input
     
     
 foreach($scope in Get-DhcpServerv4ScopeStatistics) 
 { 
     if($scope.Free -gt $Input){
         Write-Host "Scope $($scope.ScopeID) has more than $inputInt free ips"
     }
     elseif($scopeFree -lt $inputInt){
     Write-Host 'scope' $($scope.ScopeID) 'has less than' $inputint 'free ips'
     }  
 }
     
 Write-Host 'Do you want to make an ip reservation? [Y/n]'
 $input2 = Read-Host
     
     
 if($input2 -eq 'Y'){
 Write-Host 'Enter the scope you wnat to use'
 $inputScope = Read-Host
 Write-Host 'Enter a MAC-address'
 $inputMac = Read-Host
     
 $freeIP = Get-DhcpServerv4FreeIPAddress  -ScopeId $inputScope -NumAddress 1
     
 Write-Host 'A reservation is going to be made for ' $freeIP 'using scope' $inputScope 'and MAC adress' $inputMac -FO Yellow
 Write-Host 'Do you Want to make this reservation? [Y/n]'
 $input3 = Read-Host
     if($input3 -eq 'Y'){
     Add-DhcpServerv4Reservation -ScopeId $inputScope -IPAddress $freeIP -ClientId $InputMac
     Write-Host 'DHCP Reservation has been made' -FO Yellow
     }
     else{
     break
     }
 }
 else{
 break
 }
     
 Read-Host -Prompt "Press Enter to exit"

Thanks in advance!

windows-server-powershell
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

Hi,

Try appending -ErrorAction Stop to Line 32 and see if there are any error messages.

 Add-DhcpServerv4Reservation -ScopeId $inputScope -IPAddress $freeIP -ClientId $InputMac -ErrorAction Stop

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.