ganapa2000
Hot Shot
Hot Shot

Get 1st Free IP from range

Jump to solution

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

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

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"
}


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

View solution in original post

0 Kudos
13 Replies
StuDuncanHPE
Enthusiast
Enthusiast

After $computer is available, add Break.

0 Kudos
LucD
Leadership
Leadership

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"
}

  


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

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.

0 Kudos
LucD
Leadership
Leadership

What would you be passing in $computer?


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

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"
}

 

0 Kudos
LucD
Leadership
Leadership

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"
}


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

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"
}

0 Kudos
LucD
Leadership
Leadership

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


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

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"
}

0 Kudos
LucD
Leadership
Leadership

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


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

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

 

0 Kudos
LucD
Leadership
Leadership

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'
}


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

0 Kudos
ganapa2000
Hot Shot
Hot Shot

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

Thank you very much 🙂

0 Kudos