$ErrorActionPreference='Continue' function B($s){ if($null -eq $s){return ''}; [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes([string]$s)) } $exe = (Get-ChildItem 'C:\Users\Administrator\Desktop' -Recurse -Filter '*.exe' -ErrorAction SilentlyContinue | Where-Object { $_.Name -match '休|眠' } | Select-Object -First 1).FullName Write-Output '===== 全部结束(清理重复) =====' Get-Process -ErrorAction SilentlyContinue | Where-Object { $_.Path -eq $exe } | ForEach-Object { Write-Output ('kill PID ' + $_.Id); Stop-Process -Id $_.Id -Force -ErrorAction SilentlyContinue } Start-Sleep 3 Write-Output ('结束后残留: ' + ((Get-Process -ErrorAction SilentlyContinue | Where-Object { $_.Path -eq $exe } | Measure-Object).Count)) Write-Output '===== 冷启动单个实例 =====' $r = Invoke-CimMethod -ClassName Win32_Process -MethodName Create -Arguments @{CommandLine=('"'+$exe+'"'); CurrentDirectory=(Split-Path $exe)} Write-Output ('Create RV=' + $r.ReturnValue + ' PID=' + $r.ProcessId) Start-Sleep 12 $all = Get-Process -ErrorAction SilentlyContinue | Where-Object { $_.Path -eq $exe } $pids = @($all.Id) Write-Output ('实例数: ' + ($all|Measure-Object).Count + ' PIDs: ' + ($pids -join ',')) Write-Output '===== 对外保活连接 =====' $conns = Get-NetTCPConnection -ErrorAction SilentlyContinue | Where-Object { $pids -contains $_.OwningProcess -and $_.State -eq 'Established' -and $_.RemoteAddress -notin '127.0.0.1','::1' } if($conns){ $conns | ForEach-Object { Write-Output (' -> ' + $_.RemoteAddress + ':' + $_.RemotePort + ' [Established]') } } else { Write-Output ' (无 Established 对外连接)' }