Files
ctbjrj/tools/test_cleanup_ps.py

24 lines
1.1 KiB
Python

# -*- coding: utf-8 -*-
# 验证 清理代理与WebView进程 的 PowerShell 脚本(C# 里用 Base64 UTF-16LE 传递)
# 此处以完全相同的方式构建并执行,确认语法正确、能正常运行并输出计数
import base64
import subprocess
ps = (
"$n=0; "
"Get-CimInstance Win32_Process -Filter \"Name='python.exe' OR Name='pythonw.exe' OR Name='msedgewebview2.exe'\" | "
"Where-Object { $_.ProcessId -ne $pid -and ( "
"$_.CommandLine -like '*proxy_server.py*' -or "
"($_.Name -eq 'msedgewebview2.exe' -and $_.CommandLine -like '*成套报价软件.exe.WebView2*') ) } | "
"ForEach-Object { try { Stop-Process -Id $_.ProcessId -Force -ErrorAction SilentlyContinue; $n++ } catch {} }; "
"Write-Output $n"
)
enc = base64.b64encode(ps.encode("utf-16-le")).decode()
r = subprocess.run(
["powershell", "-NoProfile", "-ExecutionPolicy", "Bypass", "-EncodedCommand", enc],
capture_output=True, text=True, timeout=30,
)
print("退出码:", r.returncode)
print("输出(清理数):", r.stdout.strip())
print("错误:", r.stderr.strip()[:300] if r.stderr.strip() else "无")