Have you ever wanted to perform a certain kind of action on a range of ip addresses using powercli or powershell? Well, I have.
for ex:
foreach ($host in $hosts) {scriptblock}
and here I only have the 1st ip of the hosts and the rest should auto increment.
Well thanks to Lucd (If you have ever seen anyone answering all the powercli queries on vmware communities) we have it now.
#let's get the 1st ip address
$host1 = Read-Host "type the 1st ip address"
#Then let us have the number of ip addresses that you want
$max = Read-Host "Maximum nubmer of ip addresses?"
#now split that ip address and retain the 1st 3 octets which will be fixed
$fixed = $host1.Split('.')[0..2]
#now let us have the last octet of the host's ip address
$last = [int]($host1.Split('.')[3])
#let us subtract 1 from the maximum number of IPs that we want since it counts from 0
$max_hosts = $max - 1
#now let us increment the $last octet with a +1 till we get $max_hosts of ip addresses
$hosts =
$last..($last + $max_hosts) | %{
[string]::Join('.',$fixed) + "." + $_
}
#now let us print the results to the screen
$hosts
Get this IPincrementer on github.
for ex:
foreach ($host in $hosts) {scriptblock}
and here I only have the 1st ip of the hosts and the rest should auto increment.
Well thanks to Lucd (If you have ever seen anyone answering all the powercli queries on vmware communities) we have it now.
#let's get the 1st ip address
$host1 = Read-Host "type the 1st ip address"
#Then let us have the number of ip addresses that you want
$max = Read-Host "Maximum nubmer of ip addresses?"
#now split that ip address and retain the 1st 3 octets which will be fixed
$fixed = $host1.Split('.')[0..2]
#now let us have the last octet of the host's ip address
$last = [int]($host1.Split('.')[3])
#let us subtract 1 from the maximum number of IPs that we want since it counts from 0
$max_hosts = $max - 1
#now let us increment the $last octet with a +1 till we get $max_hosts of ip addresses
$hosts =
$last..($last + $max_hosts) | %{
[string]::Join('.',$fixed) + "." + $_
}
#now let us print the results to the screen
$hosts
Get this IPincrementer on github.