Select IP, test availability loop

Welcome Forums General PowerShell Q&A Select IP, test availability loop

Viewing 11 reply threads
  • Author
    Posts
    • #220653
      Participant
      Topics: 11
      Replies: 37
      Points: 24
      Rank: Member

      Hello,

      I’m hoping someone could provide some guidance with a script I’ve been working on.

      Objective of the script:

      • Select an IP from a .csv then test if it’s in use with a ping.
      • If the IP is in use, select another and try again.
      • If the IP is free add it to a variable.

      I’ve had success selecting an IP from the .cvs containing unused IPs, but I run into issues trying to get it to loop back if the IP is in use. I think my “elseif” is wrong and I’m also wondering if I even need a function?

      Current version:

      function get-testip {
         
              $importPath = “C:\temp\ip.csv”
              $count = 1
              $list = Import-Csv -Path $importPath | Select-Object -ExpandProperty ip
              $ip = Get-Random -InputObject $list -Count $count
              Test-Connection -ComputerName $ip -Count 3 -Quiet
      }
      If ( (get-testip) -eq $false )
          {
              Set-Variable -Name VMip -Value $ip
          }
      elseif ((get-testip) -eq $true )
          {
              get-testip
          }
    • #220656
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      John,

      this snippet would output all IPs from the list not responding to a ping:

      $CSVData = 'C:\temp\ip.csv'
      $Result = foreach ($ip in $CSVData) {
          if(-not (Test-Connection -ComputerName $ip -Quiet -Count 1)){
              $ip
              break
          }
      }
    • #220683
      Participant
      Topics: 11
      Replies: 37
      Points: 24
      Rank: Member

      Hi Olaf,

      Thanks for replying, but what you suggest is not what I’m trying to achieve.

      Objective of the script:

      • Select an IP from a .csv then test if it’s in use with a ping.
      • If the IP is in use, select another and try again.
      • If the IP is free add it to a variable.

      I only want one IP from the .csv to add to a variable as I intend to use that IP/Variable later.

    • #220704
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      Thanks for replying, but what you suggest is not what I’m trying to achieve.

      The code does exactly what you was asking for. It takes one IP from the list, checks if a device is responding from that IP and if not it outputs this IP. Then it takes the next IP and so on. If you want to collect these IPs not responding you just have to put a “$Result =” in front of the “foreach” and you’re done. What’s wrong on that?

    • #220707
      Participant
      Topics: 11
      Replies: 37
      Points: 24
      Rank: Member

      You stated “this snippet would output all IPs from the list not responding to a ping”. I only want one unused IP added to a variable. I also don’t want it to run through each IP, I want to take a random IP from the list.

    • #220710
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      . I only want one unused IP added to a variable.

      Ah ok … I got this wrong … I changed my code accordingly.

      I also don’t want it to run through each IP, I want to take a random IP from the list.

      What’s the difference? The first IP is as random as any other IP in your list. The result is you get an IP where no device is responding.

    • #220716
      Participant
      Topics: 11
      Replies: 37
      Points: 24
      Rank: Member

      If something is done in sequence it’s not random, its in sequence.

      Thanks for offering to help, but as I said, what you have provided is not what I was looking for. I was looking for guidance with what I had created, not something new that doesn’t do what I need.

    • #220719
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      OK … you want it random you get it random 😉

      $list = Import-Csv -Path $importPath | Select-Object -ExpandProperty ip
      
      do {
          $IP = $List | Get-Random -Count 1
          if(-not (Test-Connection -ComputerName $IP -Quiet -Count 1)){
              $Result = $IP
          }
      } until ($Result)

      I just hope the first IP in your list will not be mad at me now. 😉 😀

      Edit: May I ask what you need this for?

    • #221136
      Participant
      Topics: 11
      Replies: 37
      Points: 24
      Rank: Member

      Thank you Olaf, I appreciate you taking the time to help me.

      To answer your question, its part of a script to automate VM builds. I have VM build script written and it works. There are some parts that are going to vary each run, like IPs. The IPs that are used are not always in sequence, but sometimes go for a longtime in sequence. Choosing an IP randomly may have a better success rate than testing 10’s of IPs in sequence before finding one that is free. We will find a better solution in time, but this will do for now.

      May I ask why you chose to create the script the way you have? Was my original script not able to work with some small changes? Can you advise where I was going wrong?

    • #221205
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      ….
      Choosing an IP randomly may have a better success rate than testing 10’s of IPs in sequence before finding one that is free.

      Using a randomly selected element does not guarantee true randomness. In case you get the same unavailable IP addresses tested again and again and again. I’d go with the sequencial approach. 😉

      May I ask why you chose to create the script the way you have? Was my original script not able to work with some small changes? Can you advise where I was going wrong?

      I try to follow the KISS principle. I always try to keep it as simple as possible.

    • #221385
      Participant
      Topics: 11
      Replies: 37
      Points: 24
      Rank: Member

      I think get-random selecting the same IP over and over is very unlikely.

      Thanks for the answer.

       

      • This reply was modified 1 month, 1 week ago by John K. Reason: Removed question
    • #221394
      Participant
      Topics: 4
      Replies: 2247
      Points: 5,484
      Helping Hand
      Rank: Community MVP

      I think get-random selecting the same IP over and over is very unlikely.

      You never know. But it’s quite easy to test. 😉

Viewing 11 reply threads
  • You must be logged in to reply to this topic.