Last active
July 14, 2020 15:50
-
-
Save iamkilo/0763f488988ba1776189d02f03f07596 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Iterate-Numbers{ | |
Param( | |
[parameter(mandatory = $true)] | |
$prefix, | |
[parameter(mandatory = $true)] | |
$suffix, | |
[parameter(mandatory = $true)] | |
$range, | |
[switch]$singleDigits | |
) | |
$numberSplit = $range.split('-') | |
$startNumber = [int]$numberSplit[0] | |
$endNumber = [int]$numberSplit[1] | |
if ($singleDigits.IsPresent) { | |
$numberArray = $startNumber..$endNumber | |
} | |
else{ | |
$numberArray = $startNumber..$endNumber | ForEach-Object {"{0:D2}" -f $_} | |
} | |
foreach($number in $numberArray){ | |
Write-Output $prefix$number$suffix | |
} | |
} | |
function Get-IPRange | |
{ | |
param | |
( | |
[string]$start, | |
[string]$end, | |
[string]$ip, | |
[string]$mask, | |
[int]$cidr | |
) | |
function IP-toINT64 () { | |
param ($ip) | |
$octets = $ip.split(".") | |
return [int64]([int64]$octets[0]*16777216 +[int64]$octets[1]*65536 +[int64]$octets[2]*256 +[int64]$octets[3]) | |
} | |
function INT64-toIP() { | |
param ([int64]$int) | |
return (([math]::truncate($int/16777216)).tostring()+"."+([math]::truncate(($int%16777216)/65536)).tostring()+"."+([math]::truncate(($int%65536)/256)).tostring()+"."+([math]::truncate($int%256)).tostring() ) | |
} | |
if ($ip) {$ipaddr = [Net.IPAddress]::Parse($ip)} | |
if ($cidr) {$maskaddr = [Net.IPAddress]::Parse((INT64-toIP -int ([convert]::ToInt64(("1"*$cidr+"0"*(32-$cidr)),2)))) } | |
if ($mask) {$maskaddr = [Net.IPAddress]::Parse($mask)} | |
if ($ip) {$networkaddr = new-object net.ipaddress ($maskaddr.address -band $ipaddr.address)} | |
if ($ip) {$broadcastaddr = new-object net.ipaddress (([system.net.ipaddress]::parse("255.255.255.255").address -bxor $maskaddr.address -bor $networkaddr.address))} | |
if ($ip) { | |
$startaddr = IP-toINT64 -ip $networkaddr.ipaddresstostring | |
$endaddr = IP-toINT64 -ip $broadcastaddr.ipaddresstostring | |
} else { | |
$startaddr = IP-toINT64 -ip $start | |
$endaddr = IP-toINT64 -ip $end | |
} | |
for ($i = $startaddr; $i -le $endaddr; $i++) | |
{ | |
INT64-toIP -int $i | |
} | |
} | |
$serverList = Iterate-Numbers -prefix webserver -suffix .contoso.com -range "03-08" | |
$ipAddresses = Get-IPRange -start "192.168.1.80" -end "192.168.1.85" | |
[int]$max = $serverList.Count | |
if ([int]$ipAddresses.count -gt [int]$serverList.count) { $max = $ipAddresses.Count; } | |
$servers = for ( $i = 0; $i -lt $max; $i++) | |
{ | |
Write-Verbose "$($serverList[$i]),$($ipAddresses[$i])" | |
[PSCustomObject]@{ | |
name = $serverList[$i] | |
ipAddresses = $ipAddresses[$i] | |
} | |
} | |
foreach($server in $servers){ | |
New-A10Server -name $server.name -ipaddress $server.ipAddresses | |
New-A10ServerPort -server $server.name -port 80 -protocol tcp | |
New-A10ServiceGroupMember -group "Web Servers" -name $server.name -port 80 | |
} | |
$Shell = New-Object -ComObject "WScript.Shell" | |
$Button = $Shell.Popup("Are you sure you're ready to bring these servers into route?", 0, "Confirmation", 0) | |
foreach($server in $servers){ | |
Enable-A10ServiceGroupMember -group "Web Servers" -groupMember $server.name -port 80 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment