Skip to content

Instantly share code, notes, and snippets.

@qlikq
Created January 28, 2020 10:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qlikq/7ee87098132f3d71854dedc92f97aa48 to your computer and use it in GitHub Desktop.
Save qlikq/7ee87098132f3d71854dedc92f97aa48 to your computer and use it in GitHub Desktop.
Select Ips from a string constrained by string length
Function Get-IPsFromString {
<#
.SYNOPSIS
Returns chunks of ips from given string limited to $MaxLength of characters from string
.DESCRIPTION
If we have 50 ips separated by commas, this function will retrieve as many batches of
ips as there can fit inside $maxlength of character from give string.
If you have to put in some form which is limited to 255 characters ips this function
will diving the ips between
By default is assumes tring limit of 255 characters and min of MaxLength-39 to handle
ipv4 and ipv6 addresses.
Ips are supposed to come as 1 string separated by commas like so:
1.1.1.1,1.1.1.2.1.1.1.3, etc..
.EXAMPLE
Generate 20 ips and group ips in batches of maximum 60 chars per batch.
PS C:\> (1..20 |%{"192.168.1.$_"}) -join ',' | Get-IPsFromString -MaxLength 60
192.168.1.1,192.168.1.2,192.168.1.3,192.168.1.4,192.168.1.5
192.168.1.6,192.168.1.7,192.168.1.8,192.168.1.9,192.168.1.10
192.168.1.11,192.168.1.12,192.168.1.13,192.168.1.14
192.168.1.15,192.168.1.16,192.168.1.17,192.168.1.18
192.168.1.19,192.168.1.20
.NOTES
Kudos to Chris Dent for helping me out with this.
#>
[cmdletbinding()]
param (
[Parameter(Mandatory,ValueFromPipeline = $True)]
[string]$IPs,
[int]$MaxLength=255
)
$IPs -join ‘,’ -replace "(\G.{$($Maxlength-39),$MaxLength}),",’$1;’ -split ‘;’
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment