Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using namespace System.Net
# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)
$ClientIP = ($request.headers.'X-Forwarded-For' -split ':')[0]
if($request.query.ports){
$Ports = $request.query.ports -split ','
} else {
$Ports = 21,22,25,8080,3389,1234,1111,2222,3333,3389,3389,4444,5555,6666,7777,8888,9999,1234
}
function test-port ($hostname,$port,$timeout=100) {
$requestCallback = $state = $null
$client = New-Object System.Net.Sockets.TcpClient
$beginConnect = $client.BeginConnect($hostname,$port,$requestCallback,$state)
Start-Sleep -milli $timeOut
if ($client.Connected) { $open = 'open' } else { $open = 'closed' }
$client.Close()
[pscustomobject]@{
Port=$port
Status=$open}
}
$Results = $ports | ForEach-Object { Test-port -Port $_ -hostname $ClientIP}
# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."
# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = $Results
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment