Ivanti Port Check PowerShell Script

Modified on Thu, 25 May 2023 at 05:10 PM

For Ivanti Endpoint Manager it is important to have all required Ports open.

The following PowerShell scripts can test the open ports on the client as well on the core server.


The Script well generate this output and a log file:




Here is the required Powershellscript (Core to Client)

Replace the <yourCoreServerNameHere> with your core server name.
The script need to be run on the core server.


$Coreserver = "<yourCoreServerNameHere>"
$ip = "%Hostname%"
$Protocol = 3


if (!(Test-Path -path "C:\LDPorts")) {New-Item "C:\LDPorts" -Type Directory}

$datestring = (Get-Date).ToString("s").Replace(":",".")

function Test-Port{ 

[cmdletbinding( 
    DefaultParameterSetName = '', 
    ConfirmImpact = 'low' 
)] 
    Param( 
        [Parameter( 
            Mandatory = $True, 
            Position = 0, 
            ParameterSetName = '', 
            ValueFromPipeline = $True)] 
            [array]$computer, 
        [Parameter( 
            Position = 1, 
            Mandatory = $True, 
            ParameterSetName = '')] 
            [array]$port, 
        [Parameter( 
            Mandatory = $False, 
            ParameterSetName = '')] 
            [int]$TCPtimeout=1000, 
        [Parameter( 
            Mandatory = $False, 
            ParameterSetName = '')] 
            [int]$UDPtimeout=1000,             
        [Parameter( 
            Mandatory = $False, 
            ParameterSetName = '')] 
            [switch]$TCP, 
        [Parameter( 
            Mandatory = $False, 
            ParameterSetName = '')] 
            [switch]$UDP                                   
        ) 
    Begin { 
        If (!$tcp -AND !$udp) {$tcp = $True}   
        $ErrorActionPreference = "SilentlyContinue" 
        $report = @()
        $report | Format-List
    } 
    Process {     
        ForEach ($c in $computer) { 
            ForEach ($p in $port) { 
                If ($tcp) {   
                    $temp = "" | Select Port, Open
                   $tcpobject = new-Object system.Net.Sockets.TcpClient             
                    $connect = $tcpobject.BeginConnect($c,$p,$null,$null) 
                    $wait = $connect.AsyncWaitHandle.WaitOne($TCPtimeout,$false) 
                    If(!$wait) { 
                        $tcpobject.Close() 
                        $temp.Port = $p 
                        $temp.Open = "Closed or filtered" 
                    } Else { 
                        $error.Clear() 
                        try
                        {
                            $tcpobject.EndConnect($connect) | out-Null
                        }
                        catch
                        {
                            If($error[0])
                            { 
                                [string]$string = ($error[0].exception).message 
                                $message = (($string.split(":")[1]).replace('"',"")).TrimStart() 
                                $failed = $true 
                            } 
                        }
                        #Close connection     
                        $tcpobject.Close() 
                        #If unable to query port to due failure 
                        If($failed){ 
                            #Build report   
                            #$temp.Server = $c
                            $temp.Port = $p   
                            #$temp.TypePort = "TCP"
                            $temp.Open = "Closed or filtered" 
                            #$temp.Notes = "$message" 
                        } Else{ 
                            #Build report   
                            #$temp.Server = $c
                            $temp.Port = $p
                            #$temp.TypePort = "TCP"   
                            $temp.Open = "Open"   
                        } 
                    }     
                    #Reset failed value 
                    $failed = $Null     
                    #Merge temp array with report             
                    $report += $temp 
                }     
                If ($udp) { 
                    #Create temporary holder $temp = "" | Select Server, Port, TypePort, Open, Notes   
                    $temp = "" | Select Port, Open                                   
                    #Create object for connecting to port on computer 
                    $udpobject = new-Object system.Net.Sockets.Udpclient
                    #Set a timeout on receiving message
                    $udpobject.client.ReceiveTimeout = $UDPTimeout
                    #Connect to remote machine's port               
                    Write-Verbose "Making UDP connection to remote server"
                    $udpobject.Connect("$c",$p)
                    #Sends a message to the host to which you have connected.
                    Write-Verbose "Sending message to remote host"
                    $a = new-object system.text.asciiencoding
                    $byte = $a.GetBytes("$(Get-Date)")
                    [void]$udpobject.Send($byte,$byte.length)
                    #IPEndPoint object will allow us to read datagrams sent from any source. 
                    Write-Verbose "Creating remote endpoint"
                    $remoteendpoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0)
                    Try {
                        #Blocks until a message returns on this socket from a remote host.
                        Write-Verbose "Waiting for message return"
                        $receivebytes = $udpobject.Receive([ref]$remoteendpoint)
                        [string]$returndata = $a.GetString($receivebytes)
                        If ($returndata) {
                           Write-Verbose "Connection Successful" 
                            #Build report 
                            #$temp.Server = $c
                            $temp.Port = $p
                            #$temp.TypePort = "UDP"   
                            $temp.Open = "Open" 
                            #$temp.Notes = $returndata   
                            $udpobject.close()   
                        }                       
                    } Catch {
                        If ($Error[0].ToString() -match "\bRespond after a period of time\b") {
                            #Close connection 
                            $udpobject.Close() 
                            #Make sure that the host is online and not a false positive that it is open
                            If (Test-Connection -comp $c -count 1 -quiet) {
                                Write-Verbose "Connection Open" 
                                #Build report   
                                #$temp.Server = $c
                                $temp.Port = $p
                                #$temp.TypePort = "UDP"
                                $temp.Open = "Open" 
                            } Else {
                                <#
                                It is possible that the host is not online or that the host is online, 
                                but ICMP is blocked by a firewall and this port is actually open.
                                #>
                                Write-Verbose "Host maybe unavailable" 
                                #Build report   
                                #$temp.Server = $c
                                $temp.Port = $p 
                                #$temp.TypePort = "UDP" 
                                $temp.Open = "Closed or filtered" 
                                #$temp.Notes = "Unable to verify if port is open or if host is unavailable."                                 
                            }                         
                        } ElseIf ($Error[0].ToString() -match "forcibly closed by the remote host" ) {
                            #Close connection 
                            $udpobject.Close() 
                            Write-Verbose "Connection Timeout" 
                            #Build report 
                            #$temp.Server = $c
                            $temp.Port = $p
                            #$temp.TypePort = "UDP"
                            $temp.Open = "Closed or filtered" 
                            #$temp.Notes = "Connection to Port Timed Out"                         
                        } Else {                     
                            $udpobject.close()
                        }
                    }     
                    #Merge temp array with report             
                    $report += $temp
                }                                 
            } 
        }                 
    } 
    End { 
        #Generate Report
        $report
    }
}

function TCPtest()
{

$TCPresults = Test-Port -comp $ip -port 25,137,139,445,4343,9535,9593,9594,9595,9971,9972,12174,16992,16993,16994,33354 -tcp -TCPtimeout 1800
$TCPresults | Out-File $TCPtxt

}

function UDPtest() {

    Clear-Host
    Write-Host -ForegroundColor Yellow "`n`nTesting UDP ports used by LANDESK... (This operation can take up to 15sc)"
    $UDPresults = Test-Port -comp $ip -port 68,1758,9535,9595,33354,33355,38293 -udp -UDPtimeout 1800
    $UDPresults | Out-File $UDPtxt

}

function Fulltest() {

    #Clear-Host
    #Write-Host -ForegroundColor Yellow "`n`nTesting TCP and UDP ports used by LANDESK... (This operation can take up to 45sc)"
    $TCPresults = Test-Port -comp $ip -port 25,137,139,445,4343,9535,9593,9594,9595,9971,9972,12174,16992,16993,16994,33354 -tcp -TCPtimeout 1800
    $TCPresults | Out-File $TCPtxt
    $UDPresults = Test-Port -comp $ip -port 68,1758,9535,9595,33354,33355,38293 -udp -UDPtimeout 1800
    $UDPresults | Out-File $UDPtxt




    $textTCP = "=====>  TCP  <=====`n"
    foreach($lineTCP in [System.IO.File]::ReadLines($TCPtxt))
    {
          $textTCP += $lineTCP + "`n"
    }
    #Write-Host $textTCP



    $textUDP = "=====>  UDP  <=====`n"
    foreach($lineUDP in [System.IO.File]::ReadLines($UDPtxt))
    {
          $textUDP += $lineUDP + "`n"
    }
    #Write-Host $textUDP


    $EXPLORETCP = "Results are on $Coreserver in Folder: C:\LDPorts\%Hostname%\TCP"
    $EXPLOREUCP = "Results are on $Coreserver in Folder: C:\LDPorts\%Hostname%\UCP"



    $TCPAnzeige = $Message + $textTCP + $EXPLORETCP

    $UDPAnzeige = $Message + $textUDP + $EXPLOREUCP

    Write-Host $TCPAnzeige
    Write-Host $UDPAnzeige

}

Clear-Host
$ip = Read-Host "`n`nPlease enter the hostname or the IP address of the client"

$Message = "PortCheck from $Coreserver  ==>     %Hostname%  `n"


Clear-Host

if (!(Test-Path -path "C:\LDPorts\$ip")) {New-Item "C:\LDPorts\$ip" -Type Directory}
if (!(Test-Path -path "C:\LDPorts\$ip\UDP")) {New-Item "C:\LDPorts\$ip\UDP" -Type Directory}
if (!(Test-Path -path "C:\LDPorts\$ip\TCP")) {New-Item "C:\LDPorts\$ip\TCP" -Type Directory}

$TCPtxt = "C:\LDPorts\$ip\TCP\TCPportsCheck-$ip-$datestring.txt"
$UDPtxt = "C:\LDPorts\$ip\UDP\UDPportsCheck-$ip-$datestring.txt"

switch ($Protocol) {
    1{TCPtest}
    2{UDPtest}
    3{Fulltest}
}



Here is the required PowerShell script (Client to Core)

Replace <YourcorServerNameHere> with the name of your core server.
The Script need to be run on the client


$ip = "<YourcorServerNameHere>" 
$Protocol = 3 
$Message = "PortCheck from: %Hostname%   ==>    " + $ip + "`n"  
 
if (!(Test-Path -path "C:\LDPorts")) {New-Item "C:\LDPorts" -Type Directory} 
 
$datestring = (Get-Date).ToString("s").Replace(":",".") 
 
function Test-Port{  
 
[cmdletbinding(  
    DefaultParameterSetName = '',  
    ConfirmImpact = 'low'  
)]  
    Param(  
        [Parameter(  
            Mandatory = $True,  
            Position = 0,  
            ParameterSetName = '',  
            ValueFromPipeline = $True)]  
            [array]$computer,  
        [Parameter(  
            Position = 1,  
            Mandatory = $True,  
            ParameterSetName = '')]  
            [array]$port,  
        [Parameter(  
            Mandatory = $False,  
            ParameterSetName = '')]  
            [int]$TCPtimeout=1000,  
        [Parameter(  
            Mandatory = $False,  
            ParameterSetName = '')]  
            [int]$UDPtimeout=1000,              
        [Parameter(  
            Mandatory = $False,  
            ParameterSetName = '')]  
            [switch]$TCP,  
        [Parameter(  
            Mandatory = $False,  
            ParameterSetName = '')]  
            [switch]$UDP                                    
        )  
    Begin {  
        If (!$tcp -AND !$udp) {$tcp = $True}  
        $ErrorActionPreference = "SilentlyContinue"  
        $report = @() 
        $report | Format-List 
    }  
    Process {      
        ForEach ($c in $computer) {  
            ForEach ($p in $port) {  
                If ($tcp) 
{    
                    $temp = "" | Select Port, Open 
                    $tcpobject = new-Object system.Net.Sockets.TcpClient  
                    $connect = $tcpobject.BeginConnect($c,$p,$null,$null)  
                    $wait = $connect.AsyncWaitHandle.WaitOne($TCPtimeout,$false)  
                    If(!$wait) 
{  
                        $tcpobject.Close()  
                        Write-Verbose "Connection Timeout"  
                        $temp.Port = $p  
                        $temp.Open = "Closed or filtered"  
                    } Else 
                        {  
                    try 
                    { 
                         $error.Clear()  
                        $tcpobject.EndConnect($connect) | out-Null    
                    } 
                    catch 
                    { 
                            If($error[0]){  
                            [string]$string = ($error[0].exception).message  
                            $message = (($string.split(":")[1]).replace('"',"")).TrimStart()  
                            $failed = $true  
                        }  
                    }                  
                       $tcpobject.Close()  
                        If($failed) 
{  
                            $temp.Port = $p    
                            $temp.Open = "Closed or filtered"  
                        } Else{  
                            $temp.Port = $p 
                            $temp.Open = "Open"    
                        }  
                    }      
                    $failed = $Null      
                    $report += $temp  
                }      
                If ($udp) {  
                    $temp = "" | Select Port, Open                                    
                    $udpobject = new-Object system.Net.Sockets.Udpclient 
                    $udpobject.client.ReceiveTimeout = $UDPTimeout 
                    $udpobject.Connect("$c",$p) 
                    Write-Verbose "Sending message to remote host" 
                    $a = new-object system.text.asciiencoding 
                    $byte = $a.GetBytes("$(Get-Date)") 
                    [void]$udpobject.Send($byte,$byte.length) 
                    $remoteendpoint = New-Object system.net.ipendpoint([system.net.ipaddress]::Any,0) 
                    Try { 
                        Write-Verbose "Waiting for message return" 
                        $receivebytes = $udpobject.Receive([ref]$remoteendpoint) 
                        [string]$returndata = $a.GetString($receivebytes) 
                        If ($returndata) { 
                           $temp.Port = $p 
                           $temp.Open = "Open"  
                           $udpobject.close()    
                        }                        
                    } Catch { 
                        If ($Error[0].ToString() -match "\bRespond after a period of time\b") { 
                            $udpobject.Close()  
                            If (Test-Connection -comp $c -count 1 -quiet) { 
                                $temp.Port = $p 
                                $temp.Open = "Open"  
                            } Else 
{ 
                               $temp.Port = $p  
                               $temp.Open = "Closed or filtered"  
                            }                          
                        } ElseIf ($Error[0].ToString() -match "forcibly closed by the remote host" ) { 
                           $udpobject.Close()  
                            $temp.Port = $p 
                            $temp.Open = "Closed or filtered"  
                        } Else {                      
                            $udpobject.close() 
                        } 
                    }      
                    $report += $temp 
                }                                  
            }  
        }                  
    }  
    End 
{          $report 
    } 
} 
 
function Fulltest() 
{ 
$TCPresults = Test-Port -comp $ip -port 80,137,139,443,445,4343,5007,9594,9595,9982,12175,12176,16992,16993,16994,33354 -tcp -TCPtimeout 1800 
$TCPresults | Out-File $TCPtxt 
$UDPresults = Test-Port -comp $ip -port 67,69,1759,4011,9535,9595,38293 -udp -UDPtimeout 1800 
$UDPresults | Out-File $UDPtxt 
 
 
$textTCP = "=====>  TCP  <=====`n" 
<#
foreach($lineTCP in [System.IO.File]::ReadLines($TCPtxt)) 
{ 
      $textTCP += $lineTCP + "<br>" 
} 
#>
 
$textUDP = "=====>  UDP  <=====>`n" 
<#foreach($lineUDP in [System.IO.File]::ReadLines($UDPtxt)) 
{ 
      $textUDP += $lineUDP + "<br>" 
} #>
 
$EXPLORETCP = "Results are on %Hostname% in Folder: c:\LDPorts\$ip\TCP`n" 
$EXPLOREUCP = "Results are on %Hostname% in Folder: c:\\LDPorts\$ip\UCP`n" 
 
$TCPAnzeige = $Message + $textTCP + $EXPLORETCP 
$UDPAnzeige = $Message + $textUDP + $EXPLOREUCP 
 
write-host $TCPAnzeige 
write-host $UDPAnzeige 
} 
 
if (!(Test-Path -path "C:\LDPorts\$ip")) {New-Item "C:\LDPorts\$ip" -Type Directory} 
if (!(Test-Path -path "C:\LDPorts\$ip\UDP")) {New-Item "C:\LDPorts\$ip\UDP" -Type Directory} 
if (!(Test-Path -path "C:\LDPorts\$ip\TCP")) {New-Item "C:\LDPorts\$ip\TCP" -Type Directory} 
 
$TCPtxt = "C:\LDPorts\$ip\TCP\TCPportsCheck-$ip-$datestring.txt" 
$UDPtxt = "C:\LDPorts\$ip\UDP\UDPportsCheck-$ip-$datestring.txt" 
 
switch ($Protocol) { 
        1{TCPtest} 
        2{UDPtest} 
        3{Fulltest} 
} 

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article