function Get-DynamicPortFromHostname { <# .SYNOPSIS Used to convert a hostname to a dynamic port assignment. Mostly used to allow stacking multiple applications on one website hostname without breaking functionality on the same server .PARAMETER Hostname The hostname to derive a port for. .OUTPUTS Returns a single int that is between 5000 and 65535 for port assignment. #> [CmdletBinding()] [OutputType([int])] param ( [Parameter(Mandatory = $true)] $Hostname ) $logLead = (Get-LogLeadName) $output = "" $websiteBytes = [System.Text.Encoding]::UTF8.GetBytes($Hostname) $hashBytes = [System.Security.Cryptography.MD5]::Create().ComputeHash($websiteBytes) for($i = 0; $i -lt $hashBytes.Length; $i++) { $output += $hashBytes[$i].ToString("X2") } # We only want the last 4 digits of the array, that can give us up to 5 digits once converted from Hex $output = $output[-4..-1] -join '' $outputPort = [System.Convert]::ToInt32($output, 16) # Why 65535? Because that's the maximum port number for TCP ports # Honestly, if our array was FFFF it should only be 65535 anyways # We converted from Hex at 4 chars, so it should be fine here # But if something gets modified above, we could end up with bad data # Better to be defensive just in case if ($outputPort -gt 65535) { $outputPort = $outputPort - 65535 } # Why 5000? Because we decided that was a good lower threshold if ($outputPort -lt 5000) { $outputPort = $outputPort + 5000 } Write-Host "$logLead : Found result port of [$outputPort]" return $outputPort }