我在服务器上的WMI服务中发生内存泄漏,该服务器托管客户端依赖于WMI的应用程序,以便实时更新应用程序中发生的事情。随着WMI服务消耗的内存增加,服务最终变得无响应并且必须重新启动。我正在尝试开发一个可以自动执行此操作的脚本,但我遇到了以下问题:
看起来SC.EXE 不依赖于WMI,所以我可以使用它来查找进程ID,但是需要一些技巧来解析SC.EXE返回的内容,一旦我这样做,如何在不使用taskkill的情况下杀死该过程?
答案 0 :(得分:0)
脚本是批处理的。下面使用的命令列表:set,for,reg,findstr,if,sc,waitfor。这些命令都不需要WMI。
在示例输出(直接在我的W10系统中)中,请注意,重新启动服务后,WMI的PID是不同的。
set "zServices2Stop="
set "zServices2Start="
for /f "tokens=*" %%A in ('reg query hklm\system\currentcontrolset\services /s /v DependOnService ^| findstr /i "hkey winmgmt"') do (
set "zDbg=0"
set "sTmp=%%A"
if /i "!sTmp:~0,5!"=="HKEY_" (
set "sTmpService=!sTmp:~53!"
) else (
set "zServices2Stop=!zServices2Stop!,!sTmpService!"
echo ;[i] Found Service With Dependancy To winmgmt Service {!sTmpService!}
)
)
set "zServices2Stop=!zServices2Stop:~1!"
echo ;[i] List Of Dependancies: {!zServices2Stop!}
for /f "tokens=3" %%A in ('sc queryex winmgmt ^| findstr /i pid') do @echo ;[i] The PID for the "winmgmt" service before: {%%A}
for %%A in (!zServices2Stop!) do (
sc queryex "%%A" 2>&1 | findstr /i running >nul && (
set "zServices2Start=!zServices2Start!,%%A"
echo ;[i] Stopping {%%A}&sc stop "%%A" >nul 2>&1
) || (
echo ;[i] Ignoring Already Stopped Service {%%A}
)
)
set "zServices2Start=!zServices2Start:~1!"
waitfor RAN%random%%random%%random%DOM /t 2 >nul 2>&1
echo ;[i] Stopping {winmgmt}&sc stop "winmgmt" >nul
waitfor RAN%random%%random%%random%DOM /t 2 >nul 2>&1
echo ;[i] Starting {winmgmt}&sc start "winmgmt" >nul
waitfor RAN%random%%random%%random%DOM /t 2 >nul 2>&1
for %%A in (!zServices2Start!) do (
echo ;[i] Starting {%%A}&sc start "%%A" >nul
)
for /f "tokens=3" %%A in ('sc queryex winmgmt ^| findstr /i pid') do @echo ;[i] The PID for the "winmgmt" service after: {%%A}
输出将如下所示:
;[i] Found Service With Dependancy To winmgmt Service {HgClientService}
;[i] Found Service With Dependancy To winmgmt Service {iphlpsvc}
;[i] Found Service With Dependancy To winmgmt Service {vmms}
;[i] List Of Dependancies: {HgClientService,iphlpsvc,vmms}
;[i] The PID for the "winmgmt" service before: {13124}
;[i] Ignoring Already Stopped Service {HgClientService}
;[i] Stopping {iphlpsvc}
;[i] Stopping {vmms}
;[i] Stopping {winmgmt}
;[i] Starting {winmgmt}
;[i] Starting {iphlpsvc}
;[i] Starting {vmms}
;[i] The PID for the "winmgmt" service after: {12980}