Skip to main content

24 posts tagged with "windows"

View All Tags

Two ways to retrieve process id while startup a process via Windows command line

· One min read

一个特殊的机缘, 需要通过 cmd.exe 启动一个进程, 并且获取该启动后进程的 Process ID, 搜罗到两种方法:

1. 通过 wmic process call create

  • wmic 如果创建进程成功,将返回一个 ReturnValue 为 0 类 JSON 结构的输出, 从中获取 ProcessId:
<code class="shell">C:\app>cmd.exe /C wmic process call create "c:\app\cluster\GatewayServer.exe start -id 6", "c:\app"
Executing (Win32_Process)-&gt;Create()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ProcessId = 19420;
ReturnValue = 0;
};
  • wmic 如果创建进程失败, 将返回一个 ReturnValue 非 0 的输出:
<code class="shell">C:\app&gt;wmic process call create "cluster\GatewayServer.exe start -id 6", "c:\app"
Executing (Win32_Process)-&gt;Create()
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ReturnValue = 9;
};

但 wmic 有一个很大的问题: 不使用当前用户上下文和系统的环境变量.

2. 通过 powershell 的 Start-Process 启动进程, 然后取 Start-Process 返回对像的 Id 属于得到 Process ID:

<code class="shell">C:\app>cmd.exe /C powershell -Command "try{$app = Start-Process -PassThru -FilePath \"cluster\GatewayServer.exe\" -WorkingDirectory \"C:\app\" -ArgumentList \"start -id 5\";echo $app.Id} catch {throw}"

Close TCP and UDP ports via windows command line

· One min read

The CurrPorts tool from Nirsoft can easily close a TCP/UDP connection without kill the Process via Windows command line

cports.exe /close <Local Address> <Local Port> <Remote Address> <Remote Port>

Example:

<code class="shell">cports.exe /close 172.24.3.102 51512 172.25.1.206 8007

Download: CurrPorts Refer: https://superuser.com/questions/384758/how-to-kill-a-particular-tcp-connection-in-windows/384761#384761

ClustrMaps