我运行了这个批处理文件:
:START
FOR /f "tokens=2 delims=," %%a in ('typeperf "processor(_Total)%% Processor Time" -SC 1 -y ^|find ":" ') DO
(set "var=%%~na"
IF var>=30
(netsh interface teredo set state disabled
netsh interface 6to4 set state disabled
netsh interface isatap set state disabled )
IF var<30
( netsh interface teredo set state client
netsh interface 6to4 set state enabled
netsh interface isatap set state enabled
) )
goto START
我希望这个程序继续扫描CPU使用百分比,并将该值存储在整数"var"中。
现在,如果CPU使用率超过30%,我想禁用IPv6。如果CPU使用率下降到30%,我想启用IPv6。我写了上面的代码,但它不起作用。你能说出问题出在哪里吗。
如果你的netsh命令是正确的,那么这可能对你有用:
@echo off
:START
FOR /f "tokens=2 delims=," %%a in ('typeperf "processor(_Total)%% Processor Time" -SC 1 -y ^|find ":" ') DO (
echo CPU is at %%a %%
IF %%~na GTR 30 (
echo disabling
netsh interface teredo set state disabled
netsh interface 6to4 set state disabled
netsh interface isatap set state disabled
) else (
echo enabling
netsh interface teredo set state client
netsh interface 6to4 set state enabled
netsh interface isatap set state enabled
)
)
echo waiting
timeout /t 5 /nobreak >nul
goto :START
还有一种方法-使用LOGMAN,它依赖于与typeperf相同的计数器。
使用这种方法,您将不需要运行/循环批处理文件——在下面的代码完成后,任务将自动启动。
但是,您需要创建两个额外的.bat
文件来启用和禁用IPv6(在本例中为disable6.bat和enable6.bat)。或者至少创建一个接受参数并使用logman的-targ
密钥的额外bat(logman和SCHTASKS都需要管理员权限):
@echo off
SCHTASKS /create /tn disable /tr "C:disable.bat" /sc ONCE /sd 01/01/1910 /st 00:00
logman stop high_cpu 2>nul & logman delete high_cpu
logman create alert high_cpu -th "Processor(_Total)%% Processor Time>30" -tn "disable"
logman start high_cpu
SCHTASKS /create /tn enable /tr "C:enable6.bat" /sc ONCE /sd 01/01/1910 /st 00:00
logman stop low_cpu 2>nul & logman delete low_cpu 2>nul
logman create alert low_cpu -th "Processor(_Total)%% Processor Time<30" -tn "enable"
logman start low_cpu
还请检查用于禁用IPv6的NSPBIND工具。