Find Available IPs using PowerShell

I  still see engineers using ping to find available IP addresses and then changing the last octet to see if the next IP is available or not

ping2.JPG

As you can see if somebody needs to find 10 free IPs it may take a quiet sometime to find if  the IP address is available. Now let me show you two different ways you  can accomplish the same using automation.

The first way I would call it quick and dirty, because the script will give you the output on the screen and that is all you get.

After you run that script you will get a similar Output on the screen

ping3

Now the problem with that approach is that you cant manipulate the output at all. For instance, if you wanted to only get IP address Available you can not do that and exporting the output to csv file would not give you the desired state.

Now let me show you the elegant way using PsCustomObject

 

Now lets look at the output

ping4.jpg

As you can see PowerShell treats it as an object meaning that it has methods and properties, this is available because  I used “PSCustomObject”. Now if you really need to get only the Available IP addresses,  execute the following command

$vlan | Where-Object {$_.Name -eq ‘Available’}

ping5.jpg

You can also  export to a csv using export-csv cmdlet

$vlan | Export-Csv C:\Location\test.csv

ping6.jpg

When using PSCustomObject keep in mind it only work on PowerShell version 3 +

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s