Automation

 View Only
Expand all | Collapse all

Get 1st Free IP from range

StuDuncanHPE

StuDuncanHPENov 10, 2022 05:00 PM

  • 1.  Get 1st Free IP from range

    Posted Nov 10, 2022 03:36 PM

    Hi,

    I am writing a script to create a VM using customization info. From the below script, I am getting the free IP address that are available in the IP range. how can I get the first free IP, so that I can pass it to customization script

    But how can I exit the script, when as soon as the first free IP is found.

    $iprange = 10..254
    $result = Foreach ($ip in $iprange)
    {
    $computer = "192.168.1.$ip"
    $status = Test-Connection $computer -count 1 -Quiet
    if (!$status)
    {
    $computer + " - available"
    }
    }

    $result | Select -First 1



  • 2.  RE: Get 1st Free IP from range

    Posted Nov 10, 2022 05:00 PM

    After $computer is available, add Break.



  • 3.  RE: Get 1st Free IP from range

    Posted Nov 10, 2022 06:41 PM

    You could do something like this.
    It will also tell you if no free IP address is found in the range

    $ipRange = 10,254
    
    $ip = $ipRange[0]
    do{
        $computer = "192.168.1.$ip"
        $ip++
    }
    until(
        $ip -gt $ipRange[1] -or -not (Test-Connection $computer -Count 1 -Quiet)
    )
    
    if($ip -lt $iprange[1]){
        "$computer - available"
    }
    else{
        "No free IP found"
    }
    

      



  • 4.  RE: Get 1st Free IP from range

    Posted Nov 11, 2022 07:28 AM

    LucD,

    That worked, but If I pass a $computer variable as a input while creating a VM in the script and if there are no free IP available, how can I validate the variable and exit the script if the variable is null.



  • 5.  RE: Get 1st Free IP from range

    Posted Nov 11, 2022 07:31 AM

    What would you be passing in $computer?



  • 6.  RE: Get 1st Free IP from range

    Posted Nov 11, 2022 07:34 AM

    LucD,

    The available free IP would be there in $computer

    $ipRange = 10,254
    
    $ip = $ipRange[0]
    do{
        $computer = "192.168.1.$ip"
        $ip++
    }
    until(
        $ip -gt $ipRange[1] -or -not (Test-Connection $computer -Count 1 -Quiet)
    )
    
    if($ip -lt $iprange[1]){
        "$computer - available"
    }
    else{
        "No free IP found"
    }

     



  • 7.  RE: Get 1st Free IP from range
    Best Answer

    Posted Nov 11, 2022 07:40 AM

    You can use a Boolean to indicate if a free IP was found or not.
    And depending on that Boolean you can take action, or not, in a script.
    Something like this for example

    $ipRange = 10,254
    
    $ip = $ipRange[0]
    do{
        $computer = "192.168.1.$ip"
        $ip++
    }
    until(
        $ip -gt $ipRange[1] -or -not (Test-Connection $computer -Count 1 -Quiet)
    )
    
    if($ip -lt $iprange[1]){
        "$computer - available"
        $found = $true
    }
    else{
        "No free IP found"
        $found = $false
    }
    
    # Check if an IP is available
    if($found){
        # Use the value in $computer
    }
    else{
        Write-Error "No free IP found"
    }


  • 8.  RE: Get 1st Free IP from range

    Posted Nov 11, 2022 08:36 AM

    LucD,

    Thank you for your help. I had a make little change as below to get the only the free IP. Please check, if this is correct ?

    As I had to delete this line, 

    "$computer - available"

     

    $ipRange = 10,254
    $ip = $ipRange[0]
    do{
    $computer = "192.168.1.$ip"
    $ip++
    }
    until(
    $ip -gt $ipRange[1] -or -not (Test-Connection $computer -Count 1 -Quiet)
    )

    if($ip -lt $iprange[1]){
    $found = $true
    }
    else{
    "No free IP found"
    $found = $false
    }

    # Check if an IP is available
    if($found){
    # Use the value in $computer
    $computer
    }
    else{
    Write-Error "No free IP found"
    }



  • 9.  RE: Get 1st Free IP from range

    Posted Nov 11, 2022 09:35 AM

    Yes, the IP address is in the $computer variable, which you can now use for the rest of the script.
    But be sure to check the $found variable as well.
    If that says $false, no free IP address was found



  • 10.  RE: Get 1st Free IP from range

    Posted Nov 11, 2022 10:00 AM

    LucD,

    while validating found variable, I am getting blank output

    # Check if an IP is available
    if($found){
    # Use the value in $computer
    $computer = $freeip

    $freeip
    }
    else{
    Write-Error "No free IP found"
    }



  • 11.  RE: Get 1st Free IP from range

    Posted Nov 11, 2022 10:03 AM

    Where is this $freeIP coming from?
    The value is in $computer



  • 12.  RE: Get 1st Free IP from range

    Posted Nov 11, 2022 10:11 AM

    LucD,

    If I use the $computer in the script then I am not sure, if the $computer is validated for found or not, hence I created, $computer = $freeip

     



  • 13.  RE: Get 1st Free IP from range

    Posted Nov 11, 2022 10:28 AM

    But what is in $freeIP, you are assigning an empty variable to $computer.
    You should combine $found and $computer, in for example an IF-THAN-ELSE

    if($found){
      $computer
    }
    else{
      'No free IP addr found'
    }


  • 14.  RE: Get 1st Free IP from range

    Posted Nov 11, 2022 10:43 AM

    LucD, Got it. I will try integrating now and let you know.

    Thank you very much