I have a Windows service that listens on port 80 using OWIN self hosting (WebApp.Start). I want to find that service programmatically using a PowerShell script. I have the following PowerShell script:
netstat -noab | Select-String ":80 " -Context(1,0) `
| Where { $_.Context.PreContext[0].Trim().StartsWith("[") } `
| ForEach { ($_.Context.PreContext[0].Substring(2) -split ']')[0] }
However this only yields lsass.exe.
If I run netstat -noa, I get some more results, but all the processes listed are system processes (not mine) or have "Can not obtain ownership information"
when I include the -b
option
TCP 0.0.0.0:80 0.0.0.0:0 LISTENING 4
TCP 10.100.18.158:80 10.100.25.102:42967 ESTABLISHED 4
TCP 10.100.18.158:80 10.100.25.102:42968 ESTABLISHED 4
TCP 10.100.18.158:80 10.100.25.102:42974 ESTABLISHED 4
TCP 10.100.18.158:80 10.100.28.8:44763 ESTABLISHED 4
TCP 10.100.18.158:80 10.100.28.8:44764 ESTABLISHED 4
TCP 10.100.18.158:80 10.100.28.8:44765 ESTABLISHED 4
TCP 10.100.18.158:80 10.100.28.8:47400 ESTABLISHED 4
TCP [::]:80 [::]:0 LISTENING 4
I recognize that this is probably because of the way OWIN uses the HTTP listener internally.
Any way to reliably determine the process/service using the port?