VMware Cloud Community
bvi1006
Enthusiast
Enthusiast
Jump to solution

Query for IP, loop back to query if IP already exists

Hi,

I've been trying to get the logic down to do the following:

Query for an IP

Ping the IP

If exists, query for IP again

If does not exist, continue on..

Can someone help me with this? I have worked on this, but I don't believe it's worth posting my code on this one!

Thanks in advance.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

That error can appear when the Test-Connection fails to make a connection to the server or IP address.

That is the reason why I included the OnErrorAction.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
13 Replies
LucD
Leadership
Leadership
Jump to solution

Are you testing the range of IP addresses in a subnet ?

WHat exactly do you mean with "query for an IP" ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
bvi1006
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

I'm just presenting a popup requesting an IP address from the user. I want to be sure the one they are giving me has not already been taken, so I want to ping the IP. If the ping succeeds I'd like to loop back and ask the user for another IP.

Thanks...

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That can be done with the Test-Connection cmdlet.

Something like this for example

$ip = Read-Host "Enter IP address" 
while(Test-Connection -ComputerName $ip -Quiet -Count 1 -ErrorAction SilentlyContinue){
  "IP address $ip seems to be in use. try another IP address"
 
$ip = Read-Host
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
bvi1006
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

The error message I get when running this is the following:

Testing connection to computer 'x.x.x.x' failed: Error due to lack of resources.

Resource unavailable.

This happens regardless of whether I input a good IP or not. Have you seen this message?

Thanks,

Jen

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It seems something went wrong with my copy/paste of that code.

I corrected the code above. Can you try again ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
gottr
Contributor
Contributor
Jump to solution

I have been using this solution for a while.  I query whole ranges since we use vlans but you should be able to modify for you use.  This snippet will ping IPs async and if a ping is recieved it will move on to the next IP.  If a ping is not recieved then it checks if there is a DNS entry (incase the server is off or rebooting).  Hope it helps!

#vlan125   
    if ($vlan -eq 'vlan125')
    {
    $valid = $true
   
        foreach($i in 254..3)
        {
        $ip = "111.111.1.$i"
            If (Test-Connection -comp $ip -count 1 -quiet)
            {
                write-host -ForegroundColor Green "Computer online $ip"
            }
            Else
            {
                "Computer offline $ip"
                try
                {
                    $HostName = [System.Net.Dns]::GetHostByAddress($ip).HostName
                    $hostname
                }
                catch
                {
                    write-host -ForegroundColor Red "This is the $ip that will be used.";
                    $valid=$false;
                    break
                }
            }
        }
            if ($valid -eq $true)
            {
            write-host -ForegroundColor Red "Alert there are no available IP address for $vlan"
            }
        $SUB = '255.255.255.0'
        $Gateway = '111.111.1.1'
    }

bvi1006
Enthusiast
Enthusiast
Jump to solution

@Luc, no, still not working, I removed the onerror action and it worked.

0 Kudos
bvi1006
Enthusiast
Enthusiast
Jump to solution

@gottr - Thanks for the script - I am using Luc's script and will be adding the additional tests. Thanks again!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Strange, it works for me with the OnErrorAction parameter.

Are you using PowerShell v2 or v3 ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
bvi1006
Enthusiast
Enthusiast
Jump to solution

2....

0 Kudos
LucD
Leadership
Leadership
Jump to solution

This is with PS v2.

ping.png

Are you getting an error message ?

Can you include the message ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
bvi1006
Enthusiast
Enthusiast
Jump to solution

Now I am not getting the error! I did get it earlier today, and yesterday:

2117521.png

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That error can appear when the Test-Connection fails to make a connection to the server or IP address.

That is the reason why I included the OnErrorAction.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos