簡體   English   中英

如何確定 Windows 服務正在偵聽的端口?

[英]How to determine the port on which a Windows service is listening?

我運行了一個名為my-service的 Windows my-service 我知道的唯一輸入是服務名稱

我可以使用服務名稱獲取進程標識符

$id = Get-WmiObject -Class Win32_Service -Filter "name='my-service'" | Select-Object -ExpandProperty ProcessId

要獲取偵聽端口列表,我可以使用netstat ,但我所有的自定義服務的PID 4都不等於Get-WmiObject cmdlet 的服務進程標識符

netstat.exe -ano

目標是使用 PowerShell 或 CMD 根據服務名稱進程標識符而不是圖像名稱來獲取服務正在偵聽的端口。

使用Get-NetTCPConnection來識別給定進程當前正在偵聽的套接字:

Get-NetTCPConnection -OwningProcess $id -State Listen

由於您已包含標記,因此您可以在 cmd.exe 中嘗試以下想法,即 Windows 命令提示符:

For /F "Tokens=2 Delims=," %G In ('%SystemRoot%\System32\tasklist.exe /SVC /Fi "Services Eq my-service" /Fo CSV /NH 2^>NUL') Do @For /F "Tokens=3 Delims=: " %H In ('%SystemRoot%\System32\netstat.exe -ano ^| %SystemRoot%\System32\findstr.exe "[^0123456789]%~G$" ^| %SystemRoot%\System32\find.exe /V "["') Do @Echo %H

只需將my-service更改為您的“真實”服務名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM