Script is batch. List of commands used below: set, for, reg, findstr, if, else, sc, waitfor. None of those commands require WMI.
Notice in the example output (straight off my W10 system) that the PID for WMI is different after the service has been restarted.
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}
Output Will Look Like:
;[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}