KS-Soft. Network Management Solutions
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister    ProfileProfile    Log inLog in 

PS: Check TCP Port in use and running process

 
Post new topic   Reply to topic    KS-Soft Forum Index -> Library
View previous topic :: View next topic  
Author Message
sysadmber



Joined: 08 Dec 2015
Posts: 43

PostPosted: Sun Jun 03, 2018 11:20 pm    Post subject: PS: Check TCP Port in use and running process Reply with quote

Hostmon provides TCP Test by sending data to the port and checks it's answer. I needed a check whether the TCP port is open and if so by which process.

This scripts runs only locally, in my case via rma.

Here is the code:

Code:

param(
[string]$param_port,
[string]$param_proc,
[string]$param_proc_desc = "'Please provide Program Description as third parameter'"
)

If (!$param_port){
    Return "ScriptRes:Bad:Program Abort - Please provide Port number as first parameter"
}
If (!$param_proc){
    Return "ScriptRes:Bad:Program Abort - Please provide process name as second parameter"
}
$process = get-process $param_proc
If (!$process.id){
    Return "ScriptRes:Bad: " + $param_proc_desc + " is not started"
}
$netcon = Get-NetTCPConnection -LocalPort $param_port

If ($netcon.State -eq "listen"){
    if ($netcon.OwningProcess -ne $process.id){
        $proc_alt = get-process -id $netcon.OwningProcess
        Return "ScriptRes:Bad:TCP Port " + $param_port + " is not in use by " + $param_proc_desc + " but " + $proc_alt.Name + " (PID: " + $proc_alt.Id + ")"
        }
    else{
        Return "ScriptRes:Ok:TCP Port " + $param_port + " is used by " + $param_proc_desc
    }
}
else{
        Return "ScriptRes:Bad:TCP Port " + $param_port + " is not in use"
}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    KS-Soft Forum Index -> Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group

KS-Soft Forum Index