{"id":470,"date":"2023-01-29T02:29:17","date_gmt":"2023-01-28T13:29:17","guid":{"rendered":"https:\/\/www.howdoiuseacomputer.com\/?p=470"},"modified":"2023-02-26T18:02:56","modified_gmt":"2023-02-26T05:02:56","slug":"check-whether-a-port-is-open-on-a-bulk-list-of-endpoints-and-fast","status":"publish","type":"post","link":"https:\/\/www.howdoiuseacomputer.com\/index.php\/2023\/01\/29\/check-whether-a-port-is-open-on-a-bulk-list-of-endpoints-and-fast\/","title":{"rendered":"Check whether a port is open on a bulk list of endpoints &#8211; and FAST!"},"content":{"rendered":"\n<p><strong><em>Hooray and welcome to 2023! <\/em><\/strong> May all of your dreams come true this year, and if not, may we at least be sheltered from P1 outages and other disasters, either natural or man-made&#8230;!<\/p>\n\n\n\n<p class=\"has-text-align-center\">&#x1f91e;&#x1f64f;&#x1f91e;&#x1f64f;&#x1f91e;&#x1f64f;&#x1f91e;&#x1f64f;&#x1f91e;&#x1f64f;&#x1f91e;&#x1f64f;&#x1f91e;&#x1f64f;&#x1f91e;&#x1f64f;&#x1f91e;&#x1f64f; <\/p>\n\n\n\n<p><strong>Recently I needed to see if RDP was open on hundreds of machines. <\/strong>No trouble I thought, and wrote a wee script that looped through a list of names or IPs using Test-NetConnection with the -Port switch. <\/p>\n\n\n\n<p>This worked no problem, BUT&#8230; Test-NetConnection can take 15+ seconds per machine (especially when waiting to &#8216;time out&#8217; if the port is closed). Not good enough, we want it to be faster!<\/p>\n\n\n\n<p><strong>Unfortunately<\/strong>, Test-NetConnection does not currently support any switches to control how long it waits for a response, although I&#8217;m sure they will add it at some point.<\/p>\n\n\n\n<p>So, a Googling we go then!  And again, I am reminded of my average intelligence when it comes to PowerShell.  But that&#8217;s okay &#8211; learning all the time is a good thing, right? &#x1f61c;<\/p>\n\n\n\n<p><strong>I found this little beowtae of a function<\/strong> called Test-PortScan which uses the TcpClient object to do the job faster.  The original is found <a href=\"https:\/\/web.archive.org\/web\/20150405035615\/http:\/\/poshcode.org\/85\">here<\/a>. Thanks to the author &#8216;BSonPosh&#8217;. &#x1f44c;<\/p>\n\n\n\n<p>As I created this script I also thought <em>&#8220;I&#8217;d like any functions to be at the bottom of the script, so the main editable code is at the top&#8221;<\/em>. Turns out this is quite easy to do by storing the main code in a variable, then calling it after the functions.  Full script below; you&#8217;ll want to edit the variables at the top &#8211; Name of the service or test, the port number, the timeout value (I went with half a second) and change the import and logfile locations if required.  Cheers. Simon! &#x1f91f;&#x1f642;&#x1f91f;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># uses test-portscan function to quickly check whether a port is open on multiple IP4 endpoints\n\n$mainblock = {\nClear-Host\n\n# set variables\n$logfile = 'c:\\temp\\portcheck_log.txt'\n$servicename = 'HTTP'\n$portnumber = '80'\n$timeoutmilliseconds = '500'\n\n# import single column of endpoints from a file (no header required)\n$endpoints = Get-Content C:\\Temp\\endpoints.txt -Force\n\n# or use an array\n# $endpoints = @(\n# \"192.168.1.254\"\n# \"1.1.1.1\"\n# \"8.8.8.8\"\n# \"www.microsoft.com\"\n# \"www.bleepingcomputer.com\"\n# )\n\n# create log file\n$null = New-Item -ItemType File -Path $logfile -Force\n\n# perform the checks\nforeach ( $endpoint in $endpoints ) {\n\n    Write-Host \"Testing $testname access to $endpoint...\"\n\n    if ((Test-PortScan -Devices $endpoint -StartPort $portnumber -EndPort $portnumber -Timeout $timeoutmilliseconds).Open -ne 'False') {\n       Write-Host -ForegroundColor Gray \"$testname is not open on $endpoint.\"\n    }\n        else {\n            Write-Host -ForegroundColor Green \"$testname is open on $endpoint! - adding to $logfile\"\n            $output = \"$testname is open on $endpoint.\"\n            $output | Out-File $logfile -Append -Force\n        }\n}\n\n# end of mainblock\n}\n\n# port checking function\nfunction Test-PortScan\n{   \n    &#91;CmdletBinding()]\n    param\n    (\n        &#91;Parameter(Mandatory = $true,\n                   ValueFromPipeline = $true,\n                   ValueFromPipelineByPropertyName = $true,\n                   Position = 0,\n                   HelpMessage = 'You must supply at least a device name or IP')]\n        &#91;Alias('Name')]\n        &#91;string]$Devices,\n        &#91;Parameter(Position = 1)]\n        &#91;int]$StartPort = 1,\n        &#91;Parameter(Position = 2)]\n        &#91;int]$EndPort = $StartPort,\n        &#91;Parameter(Position = 3)]\n        &#91;int]$Timeout = 100\n    )\n\n    BEGIN\n    {\n        $PSObject = @()\n        $Output = @()\n    }\n    PROCESS\n    {\n        foreach ($Device in $Devices)\n        {\n            Write-Verbose \"Not to the port loop yet\"\n            $Port = $StartPort\n            do\n            {\n                #($Port = $StartPort; $Port -gt $EndPort; $Port++)              \n                Write-Verbose \"Made it to the port loop\"\n                $requestCallback = $state = $null\n                $client = New-Object System.Net.Sockets.TcpClient\n                $beginConnect = $client.BeginConnect($Device, $Port, $requestCallback, $state)\n                Start-Sleep -Milliseconds $Timeout\n                if ($client.Connected) { $Open = $true }\n                else { $Open = $false }\n                $client.Close()\n                &#91;pscustomobject]@{\n                    'Computer' = $Device\n                    'Port'     = $Port\n                    'Open'     = $Open\n                }\n                $Port += 1\n            }\n            until ($Port -gt $EndPort)\n        }\n        \n    }\n    END\n    {\n        \n    }\n}\n\n# run mainblock\n&amp; $mainblock\n\n# end\n<\/code><\/pre>\n<div class=\"pvc_clear\"><\/div><p id=\"pvc_stats_470\" class=\"pvc_stats all  \" data-element-id=\"470\" style=\"\"><i class=\"pvc-stats-icon small\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/www.howdoiuseacomputer.com\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p><div class=\"pvc_clear\"><\/div>","protected":false},"excerpt":{"rendered":"<p>Hooray and welcome to 2023! May all of your dreams come true this year, and if not, may we at least be sheltered from P1 outages and other disasters, either natural or man-made&#8230;! &#x1f91e;&#x1f64f;&#x1f91e;&#x1f64f;&#x1f91e;&#x1f64f;&#x1f91e;&#x1f64f;&#x1f91e;&#x1f64f;&#x1f91e;&#x1f64f;&#x1f91e;&#x1f64f;&#x1f91e;&#x1f64f;&#x1f91e;&#x1f64f; Recently I needed to see if RDP was open on hundreds of machines. No trouble I thought, and wrote a wee [&hellip;]<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_470\" class=\"pvc_stats all  \" data-element-id=\"470\" style=\"\"><i class=\"pvc-stats-icon small\" aria-hidden=\"true\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" version=\"1.0\" viewBox=\"0 0 502 315\" preserveAspectRatio=\"xMidYMid meet\"><g transform=\"translate(0,332) scale(0.1,-0.1)\" fill=\"\" stroke=\"none\"><path d=\"M2394 3279 l-29 -30 -3 -207 c-2 -182 0 -211 15 -242 39 -76 157 -76 196 0 15 31 17 60 15 243 l-3 209 -33 29 c-26 23 -41 29 -80 29 -41 0 -53 -5 -78 -31z\"\/><path d=\"M3085 3251 c-45 -19 -58 -50 -96 -229 -47 -217 -49 -260 -13 -295 52 -53 146 -42 177 20 16 31 87 366 87 410 0 70 -86 122 -155 94z\"\/><path d=\"M1751 3234 c-13 -9 -29 -31 -37 -50 -12 -29 -10 -49 21 -204 19 -94 39 -189 45 -210 14 -50 54 -80 110 -80 34 0 48 6 76 34 21 21 34 44 34 59 0 14 -18 113 -40 219 -37 178 -43 195 -70 221 -36 32 -101 37 -139 11z\"\/><path d=\"M1163 3073 c-36 -7 -73 -59 -73 -102 0 -56 133 -378 171 -413 34 -32 83 -37 129 -13 70 36 67 87 -16 290 -86 209 -89 214 -129 231 -35 14 -42 15 -82 7z\"\/><path d=\"M3689 3066 c-15 -9 -33 -30 -42 -48 -48 -103 -147 -355 -147 -375 0 -98 131 -148 192 -74 13 15 57 108 97 206 80 196 84 226 37 273 -30 30 -99 39 -137 18z\"\/><path d=\"M583 2784 c-38 -19 -67 -74 -58 -113 9 -42 211 -354 242 -373 16 -10 45 -18 66 -18 51 0 107 52 107 100 0 39 -1 41 -124 234 -80 126 -108 162 -133 173 -41 17 -61 16 -100 -3z\"\/><path d=\"M4250 2784 c-14 -9 -74 -91 -133 -183 -95 -150 -107 -173 -107 -213 0 -55 33 -94 87 -104 67 -13 90 8 211 198 130 202 137 225 78 284 -27 27 -42 34 -72 34 -22 0 -50 -8 -64 -16z\"\/><path d=\"M2275 2693 c-553 -48 -1095 -270 -1585 -649 -135 -104 -459 -423 -483 -476 -23 -49 -22 -139 2 -186 73 -142 361 -457 571 -626 285 -228 642 -407 990 -497 242 -63 336 -73 660 -74 310 0 370 5 595 52 535 111 1045 392 1455 803 122 121 250 273 275 326 19 41 19 137 0 174 -41 79 -309 363 -465 492 -447 370 -946 591 -1479 653 -113 14 -422 18 -536 8z m395 -428 c171 -34 330 -124 456 -258 112 -119 167 -219 211 -378 27 -96 24 -300 -5 -401 -72 -255 -236 -447 -474 -557 -132 -62 -201 -76 -368 -76 -167 0 -236 14 -368 76 -213 98 -373 271 -451 485 -162 444 86 934 547 1084 153 49 292 57 452 25z m909 -232 c222 -123 408 -262 593 -441 76 -74 138 -139 138 -144 0 -16 -233 -242 -330 -319 -155 -123 -309 -223 -461 -299 l-81 -41 32 46 c18 26 49 83 70 128 143 306 141 649 -6 957 -25 52 -61 116 -79 142 l-34 47 45 -20 c26 -10 76 -36 113 -56z m-2057 25 c-40 -58 -105 -190 -130 -263 -110 -324 -59 -707 132 -981 25 -35 42 -64 37 -64 -19 0 -241 119 -326 174 -188 122 -406 314 -532 468 l-58 71 108 103 c185 178 428 349 672 473 66 33 121 60 123 61 2 0 -10 -19 -26 -42z\"\/><path d=\"M2375 1950 c-198 -44 -350 -190 -395 -379 -18 -76 -8 -221 19 -290 114 -284 457 -406 731 -260 98 52 188 154 231 260 27 69 37 214 19 290 -38 163 -166 304 -326 360 -67 23 -215 33 -279 19z\"\/><\/g><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/www.howdoiuseacomputer.com\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[8],"tags":[110,122,109,112,111,108,105,107,123,5,114,106,115],"class_list":["post-470","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-bulk","tag-check","tag-closed","tag-endpoints","tag-multiple","tag-open","tag-ping","tag-port","tag-ports","tag-powershell","tag-servers","tag-test","tag-workstations"],"post_mailing_queue_ids":[],"_links":{"self":[{"href":"https:\/\/www.howdoiuseacomputer.com\/index.php\/wp-json\/wp\/v2\/posts\/470"}],"collection":[{"href":"https:\/\/www.howdoiuseacomputer.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.howdoiuseacomputer.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.howdoiuseacomputer.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.howdoiuseacomputer.com\/index.php\/wp-json\/wp\/v2\/comments?post=470"}],"version-history":[{"count":2,"href":"https:\/\/www.howdoiuseacomputer.com\/index.php\/wp-json\/wp\/v2\/posts\/470\/revisions"}],"predecessor-version":[{"id":473,"href":"https:\/\/www.howdoiuseacomputer.com\/index.php\/wp-json\/wp\/v2\/posts\/470\/revisions\/473"}],"wp:attachment":[{"href":"https:\/\/www.howdoiuseacomputer.com\/index.php\/wp-json\/wp\/v2\/media?parent=470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.howdoiuseacomputer.com\/index.php\/wp-json\/wp\/v2\/categories?post=470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.howdoiuseacomputer.com\/index.php\/wp-json\/wp\/v2\/tags?post=470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}