{"id":165,"date":"2017-10-10T12:24:01","date_gmt":"2017-10-10T11:24:01","guid":{"rendered":"http:\/\/brgeek.com.br\/wordpress\/?p=165"},"modified":"2017-10-10T12:24:01","modified_gmt":"2017-10-10T11:24:01","slug":"check-citrix-ports-powershell","status":"publish","type":"post","link":"http:\/\/brgeek.com.br\/wordpress\/2017\/10\/10\/check-citrix-ports-powershell\/","title":{"rendered":"Check Citrix Ports Powershell"},"content":{"rendered":"<p>Hi Folks, I&#8217;ve been using this script to check HDX ports for Citrix but pretty much can be used in any situation you want to check a remote port on a server either TCP or UDP.<\/p>\n<pre><code class=\"language-powershell\" data-line=\"\">\n\n\nfunction Check-Port{  \n&lt;#    \n.SYNOPSIS    \n    Tests port on a remote server.  \n       \n.PARAMETER UDPTimeOut \n    Sets a timeout for UDP port query. (In milliseconds, Default is 1000)  \n      \n.PARAMETER TCPTimeOut \n    Sets a timeout for TCP port query. (In milliseconds, Default is 1000)\n                 \n.NOTES    \n    Name: Check-Port.ps1  \n    Author: Michael  \n    DateCreated: 18Aug2015   \n           \n.EXAMPLE    \n    Check-Port -computer &#039;server&#039; -port 80  \n  \n    \n.EXAMPLE    \n    &#039;server&#039; | Check-Port -port 80  \n   \n      \n.EXAMPLE    \n    Check-Port -computer @(&quot;server1&quot;,&quot;server2&quot;) -port 80  \n     \n    \n.EXAMPLE\n    Check-Port -comp dc1 -port 17 -udp -UDPtimeout 10000\n          \n.EXAMPLE    \n    @(&quot;server1&quot;,&quot;server2&quot;) | Check-Port -port 80  \n   \n      \n.EXAMPLE    \n    (Get-Content hosts.txt) | Check-Port -port 80  \n   \n     \n.EXAMPLE    \n    Check-Port -computer (Get-Content hosts.txt) -port 80  \n   \n        \n.EXAMPLE    \n    Check-Port -computer (Get-Content hosts.txt) -port @(1..64400)  \n    Checks a range of ports from 1-64400 on all servers in the hosts.txt file      \n            \n#&gt;   \n[cmdletbinding(  \n    DefaultParameterSetName = &#039;&#039;,  \n    ConfirmImpact = &#039;low&#039;  \n)]  \n    Param(  \n        [Parameter(  \n            Mandatory = $True,  \n            Position = 0,  \n            ParameterSetName = &#039;&#039;,  \n            ValueFromPipeline = $True)]  \n            [array]$computer,  \n        [Parameter(  \n            Position = 1,  \n            Mandatory = $True,  \n            ParameterSetName = &#039;&#039;)]  \n            [array]$port,  \n        [Parameter(  \n            Mandatory = $False,  \n            ParameterSetName = &#039;&#039;)]  \n            [int]$TCPtimeout=1000,  \n        [Parameter(  \n            Mandatory = $False,  \n            ParameterSetName = &#039;&#039;)]  \n            [int]$UDPtimeout=1000,             \n        [Parameter(  \n            Mandatory = $False,  \n            ParameterSetName = &#039;&#039;)]  \n            [switch]$TCP,  \n        [Parameter(  \n            Mandatory = $False,  \n            ParameterSetName = &#039;&#039;)]  \n            [switch]$UDP                                    \n        )  \n    Begin {  \n        If (!$tcp -AND !$udp) {$tcp = $True}  \n        #Typically you never do this, but in this case I felt it was for the benefit of the function  \n        #as any errors will be noted in the output of the report          \n        $ErrorActionPreference = &quot;SilentlyContinue&quot;  \n        $report = @()  \n    }  \n    Process {     \n        ForEach ($c in $computer) {  \n            ForEach ($p in $port) {  \n                If ($tcp) {    \n                    #Create temporary holder   \n                    $temp =  @&quot;\n&lt;style&gt;\nTABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}\nTH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}\nTD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}\n.odd  { background-color:#ffffff; }\n.even { background-color:#dddddd; }\n&lt;\/style&gt;\n&lt;title&gt;\nCitrix server Port Test\n&lt;\/title&gt;\n\n&quot;@ | Select Server, Port, TypePort, Open, Notes \n\n\n\n                    #Create object for connecting to port on computer  \n                    $tcpobject = new-Object system.Net.Sockets.TcpClient  \n                    #Connect to remote machine&#039;s port                \n                    $connect = $tcpobject.BeginConnect($c,$p,$null,$null)  \n                    #Configure a timeout before quitting  \n                    $wait = $connect.AsyncWaitHandle.WaitOne($TCPtimeout,$false)  \n                    #If timeout  \n                    If(!$wait) {  \n                        #Close connection  \n                        $tcpobject.Close()  \n                        Write-Verbose &quot;Connection Timeout&quot;  \n                        #Build report  \n                        $temp.Server = $c  \n                        $temp.Port = $p  \n                        $temp.TypePort = &quot;TCP&quot;  \n                        $temp.Open = &quot;False&quot;  \n                        $temp.Notes = &quot;Connection to Port Timed Out&quot;  \n                    } Else {  \n                        $error.Clear()  \n                        $tcpobject.EndConnect($connect) | out-Null  \n                        #If error  \n                        If($error[0]){  \n                            #Begin making error more readable in report  \n                            [string]$string = ($error[0].exception).message  \n                            $message = (($string.split(&quot;:&quot;)[1]).replace(&#039;&quot;&#039;,&quot;&quot;)).TrimStart()  \n                            $failed = $true  \n                        }  \n                        #Close connection      \n                        $tcpobject.Close()  \n                        #If unable to query port to due failure  \n                        If($failed){  \n                            #Build report  \n                            $temp.Server = $c  \n                            $temp.Port = $p  \n                            $temp.TypePort = &quot;TCP&quot;  \n                            $temp.Open = &quot;False&quot;  \n                            $temp.Notes = &quot;$message&quot;  \n                        } Else{  \n                            #Build report  \n                            $temp.Server = $c  \n                            $temp.Port = $p  \n                            $temp.TypePort = &quot;TCP&quot;  \n                            $temp.Open = &quot;True&quot;    \n                            $temp.Notes = &quot;&quot;  \n                        }  \n                    }     \n                    #Reset failed value  \n                    $failed = $Null      \n                    #Merge temp array with report              \n                    $report += $temp  \n                }      \n                If ($udp) {  \n                    #Create temporary holder   \n                    $temp = &quot;&quot; | Select Server, Port, TypePort, Open, Notes                                     \n                    #Create object for connecting to port on computer  \n                    $udpobject = new-Object system.Net.Sockets.Udpclient\n                    #Set a timeout on receiving message \n                    $udpobject.client.ReceiveTimeout = $UDPTimeout \n                    #Connect to remote machine&#039;s port                \n                    Write-Verbose &quot;Making UDP connection to remote server&quot; \n                    $udpobject.Connect(&quot;$c&quot;,$p) \n                    #Sends a message to the host to which you have connected. \n                    Write-Verbose &quot;Sending message to remote host&quot; \n                    $a = new-object system.text.asciiencoding \n                    $byte = $a.GetBytes(&quot;$(Get-Date)&quot;) \n                    [void]$udpobject.Send($byte,$byte.length) \n                    #IPEndPoint object will allow us to read datagrams sent from any source.  \n                    Write-Verbose &quot;Creating remote endpoint&quot; \n                    $remoteendpoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0) \n                    Try { \n                        #Blocks until a message returns on this socket from a remote host. \n                        Write-Verbose &quot;Waiting for message return&quot; \n                        $receivebytes = $udpobject.Receive([ref]$remoteendpoint) \n                        [string]$returndata = $a.GetString($receivebytes)\n                        If ($returndata) {\n                           Write-Verbose &quot;Connection Successful&quot;  \n                            #Build report  \n                            $temp.Server = $c  \n                            $temp.Port = $p  \n                            $temp.TypePort = &quot;UDP&quot;  \n                            $temp.Open = &quot;True&quot;  \n                            $temp.Notes = $returndata   \n                            $udpobject.close()   \n                        }                       \n                    } Catch { \n                        If ($Error[0].ToString() -match &quot;\\bRespond after a period of time\\b&quot;) { \n                            #Close connection  \n                            $udpobject.Close()  \n                            #Make sure that the host is online and not a false positive that it is open \n                            If (Test-Connection -comp $c -count 1 -quiet) { \n                                Write-Verbose &quot;Connection Open&quot;  \n                                #Build report  \n                                $temp.Server = $c  \n                                $temp.Port = $p  \n                                $temp.TypePort = &quot;UDP&quot;  \n                                $temp.Open = &quot;True&quot;  \n                                $temp.Notes = &quot;&quot; \n                            } Else { \n                                &lt;# \n                                It is possible that the host is not online or that the host is online,  \n                                but ICMP is blocked by a firewall and this port is actually open. \n                                #&gt; \n                                Write-Verbose &quot;Host maybe unavailable&quot;  \n                                #Build report  \n                                $temp.Server = $c  \n                                $temp.Port = $p  \n                                $temp.TypePort = &quot;UDP&quot;  \n                                $temp.Open = &quot;False&quot;  \n                                $temp.Notes = &quot;Unable to verify if port is open or if host is unavailable.&quot;                                 \n                            }                         \n                        } ElseIf ($Error[0].ToString() -match &quot;forcibly closed by the remote host&quot; ) { \n                            #Close connection  \n                            $udpobject.Close()  \n                            Write-Verbose &quot;Connection Timeout&quot;  \n                            #Build report  \n                            $temp.Server = $c  \n                            $temp.Port = $p  \n                            $temp.TypePort = &quot;UDP&quot;  \n                            $temp.Open = &quot;False&quot;  \n                            $temp.Notes = &quot;Connection to Port Timed Out&quot;                         \n                        } Else {                      \n                            $udpobject.close() \n                        } \n                    }     \n                    #Merge temp array with report              \n                    $report += $temp  \n                }                                  \n            }  \n        }                  \n    }  \n    End {  \n        #Generate Report  \n        $report \n    }\n}\n\nCheck-Port -computer localhost -port @(&quot;1494&quot;,&quot;2598&quot;)\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Hi Folks, I&#8217;ve been using this script to check HDX ports for Citrix but pretty much can be used in any situation you want to check a remote port on a server either TCP or UDP. function Check-Port{ &lt;# .SYNOPSIS Tests port on a remote server. .PARAMETER UDPTimeOut Sets a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":148,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[8,12,6,11,7],"class_list":["post-165","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-michael-goulart","tag-microsoft","tag-powershell","tag-script","tag-windows-updates"],"jetpack_featured_media_url":"http:\/\/brgeek.com.br\/wordpress\/wp-content\/uploads\/2017\/09\/powershell-tips-and-tricks_w_640.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/165","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/comments?post=165"}],"version-history":[{"count":1,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/165\/revisions"}],"predecessor-version":[{"id":166,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/posts\/165\/revisions\/166"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/media?parent=165"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/categories?post=165"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/brgeek.com.br\/wordpress\/wp-json\/wp\/v2\/tags?post=165"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}