question

Proxiuz-8366 avatar image
0 Votes"
Proxiuz-8366 asked AndreasBaumgarten commented

How do I ask the DHCP scope if the free IPs are higher or lower than the input number? (Poweshell)

Hello!

I want to be able to type in a number in the prompt and see which of my scopes has that amount of free IP addresses. I have come this far...

 #----------------CODE---------------------
 Write-Host "Enter number off IP-addresses: " -FO blue
 $input = Read-Host
    
    
 $SCOPES = Get-DhcpServerv4Scope
 $count = 0
    
    
 foreach($scope in $SCOPES)
        
     {
    
     $scopeID = $scope.ScopeID
     $freeIP = Get-DhcpServerv4FreeIPAddress -ScopeID $scope.ScopeID -NumAddress 1024
     foreach  ($freeIP in $scope)
         {
         $count ++ 
                 
         }
    
         if($count -lt $input)
             {
                 Write-Host $scopeID " has less than  " $input " free IP addresses." 
             }
                
             elseif($count -gt $input)
                 {
                     Write-Host $scopeID " has more than  " $input " free IP addresses." 
                 }
    
    
      }
 #--------------END OF CODE

But it dosent seems to work as intended. Seems like the scipt has trouble of counting the free IPs?

Thanks in advance.

windows-server-powershell
· 1
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.

Hi @Proxiuz-8366 ,

are there any additional questions? Does the answer help to solve the issue?
If you found the answer helpful, it would be great if you please mark it "Accept as answer". This will help others to find answers in Q&A


Regards
Andreas Baumgarten

0 Votes 0 ·
AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered Proxiuz-8366 commented

Hi @Proxiuz-8366 ,

not tested by myself ... but in my opinion the $count should be reset for each $scope. Otherwise $count will increase to total sum for all IPs in all scopes, and not a count per scope.

Please try this and see if the count works like expected.

 #----------------CODE---------------------
 Write-Host "Enter number off IP-addresses: " -FO blue
 $input = Read-Host
           
 $SCOPES = Get-DhcpServerv4Scope
         
 foreach ($scope in $SCOPES) {
     $count = 0
     $scopeID = $scope.ScopeID
     $freeIP = Get-DhcpServerv4FreeIPAddress -ScopeID $scope.ScopeID -NumAddress 1024
     foreach ($freeIP in $scope) {
         $count ++ 
     }
        
     if ($count -lt $input) {
         Write-Host $scopeID " has less than  " $input " free IP addresses." 
     }
                    
     elseif ($count -gt $input) {
         Write-Host $scopeID " has more than  " $input " free IP addresses." 
     }
           
 }
 #--------------END OF CODE


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten


· 1
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.

Hmmm...
If I use that code the output is:

"
WARNING: The requested number of free IP addresses could not be found.
WARNING: The requested number of free IP addresses could not be found.
WARNING: The requested number of free IP addresses could not be found.
"

It dosent tell me wether its more or less.

I figured I maybe should use Get-DhcpServerv4ScopeStatistics and get the information from there instead somehow.

0 Votes 0 ·
AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered AndreasBaumgarten edited

Hi @Proxiuz-8366 ,

the reason for the output might be because you are using the -NumAddress 1024. I couldn't try this because I don't have a DHCP Server running here.
But my guess is the command is looking for 1024 free IP addresses and can't find the amount of free IP Addresses.

Using Get-DhcpServerv4ScopeStatistics might be good option.

Not tested by myself:

 #----------------CODE---------------------
 Write-Host "Enter number of IP-addresses: " -FO blue
 $input = Read-Host
           
 $SCOPES = Get-DhcpServerv4Scope
         
 foreach ($scope in $SCOPES) {
     $scopeID = $scope.ScopeID
     $freeIP = (Get-DhcpServerv4ScopeStatistics -ComputerName "dhcpServer.yourdoman.local" -ScopeId $scopeID).AddressesAvailable
     if ($freeIP -lt $input) {
         Write-Host $scopeID " has less than  " $input " free IP addresses." 
     }
                    
     elseif ($freeIP -gt $input) {
         Write-Host $scopeID " has more than  " $input " free IP addresses." 
     }    
 }
 #--------------END OF CODE

As I couldn't test the script here:
Maybe it's AddressAvailable or Free or AddressesFree . I found different screenshots of the result output.


(If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

Regards
Andreas Baumgarten

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.

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

Hi,

Why not use the "count" property of $freeip?

 Write-Host "Enter number off IP-addresses: " -FO blue
 $input = Read-Host 
 $SCOPES = Get-DhcpServerv4Scope    
 foreach($scope in $SCOPES)
 {    
     $scopeID = $scope.ScopeID
     $freeIP = Get-DhcpServerv4FreeIPAddress -ScopeID $scope.ScopeID -NumAddress 1024
         
     if($freeIP.count -lt $input)
     {
         Write-Host $scopeID " has less than  " $input " free IP addresses." 
     }
     elseif($freeIP.count -gt $input)
     {
         Write-Host $scopeID " has more than  " $input " free IP addresses." 
     }
 }

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.