forked from origadmin/shadcn-admin-design
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkill.ps1
More file actions
30 lines (26 loc) · 965 Bytes
/
kill.ps1
File metadata and controls
30 lines (26 loc) · 965 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 检查是否提供了端口号参数
if (-not $args -or $args.Length -ne 1) {
Write-Output "Usage: kill-port-process.ps1 <port>"
exit 1
}
# 获取端口号参数
$port = $args[0]
# 查找占用指定端口的进程ID
$tcpConnections = Get-NetTCPConnection -LocalPort $port -ErrorAction SilentlyContinue
if ($tcpConnections) {
$processIds = $tcpConnections | Where-Object { $_.State -eq 'Listen' } | Select-Object -ExpandProperty OwningProcess -Unique
if ($processIds) {
foreach ($processId in $processIds) {
try {
Stop-Process -Id $processId -Force
Write-Output "Terminated process with PID ${processId}."
} catch {
Write-Output "Failed to terminate process with PID ${processId}: $_"
}
}
} else {
Write-Output "No process is using port $port in 'Listen' state."
}
} else {
Write-Output "No process is using port $port."
}