[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[网络连接] 【求助】检测网速然后自动休眠的脚本

本帖最后由 435718680 于 2021-12-13 14:48 编辑

求写个 检测网速并自动系统休眠的脚本,有一个win10平板下载的dontsleep用不了,不能阻止平板休眠。
http://www.softwareok.com/?seite=Microsoft/DontSleep
所以只能手动切换休眠,网上找的命令行
永不休眠
powercfg /change standby-timeout-ac 0
powercfg /change standby-timeout-dc 0
3分钟休眠
powercfg /change standby-timeout-ac 3
powercfg /change standby-timeout-dc 3

求写个脚本检测电脑当前下载速度,当下载速度一直低于某值(20kb)一段时间,比如20分钟,切换为3分钟休眠。
如果脚本运行前能读取当前电脑休眠设置参数powercfg /change standby-timeout-ac 3更好,如启动脚本读取powercfg /change standby-timeout-ac 3则设置powercfg /change standby-timeout-ac 0以免下载时休眠导致中断下载。之后再判断 当下载速度一直低于某值(20kb)一段时间,比如20分钟,切换为3分钟休眠。

回复 8# flashercs


    完美使用,谢谢

TOP

回复 7# 435718680


    源代码已修改.
微信:flashercs
QQ:49908356

TOP

本帖最后由 435718680 于 2021-12-13 15:09 编辑

回复 6# flashercs


    你好,谢谢大神帮助,目前可以稳定使用,配合WScript.Sleep 60000 可以实现一分钟一次检测,20分钟休眠,也可以减少窗口打印信息过于密集的问题
    但是,你能不能修改一下,那个打印网速自动识别为kb/s或者mb/s甚至gb/s而且带上单位不用kbps而用kb/s呢?这样显示窗口还可以兼做网速监测。而且我自己修改speed = speed * 8  为speed = speed / 1024 会导致小数点很长,非常难看,最好能去掉小数点后面的数据,方便观测。
    还有就是能不能做个设置用来开启关闭网速窗口?有时候可以不用窗口也行,就在后台运行就可以了,我自己改不出来,  去掉  WScript.Echo "speed = " & speed就运行不成功了。以前找的那个就不用显示窗口可以运行。

    如果都能实现那就完美了,再次感谢大神帮助

TOP

本帖最后由 flashercs 于 2021-12-13 16:34 编辑

回复 5# 435718680
vbs脚本
  1. Option Explicit
  2. On Error Resume Next
  3. ' Const conBPSReceived = "BytesReceivedPersec"
  4. ' Const conBPSSent = "BytesSentPersec"
  5. ' Const conBPSTotal = "BytesTotalPersec"
  6. Const conInterval = 1200 ' 时间间隔(秒)
  7. Const conSpeedMax = 20000 ' 网速上限bps(bits/s)
  8. Const conFrequency = 5000 ' 监控网速间隔(毫秒)
  9. Const conShowWindow = True ' 是否显示窗口;显示=True, 隐藏=False
  10. Main
  11. Sub Main()
  12.   On Error Resume Next
  13.   RunasAdmin
  14.   If conShowWindow Then
  15.     RunasScriptHost "cscript.exe"
  16.   Else
  17.     RunasScriptHost "wscript.exe"
  18.   End If
  19.   Dim strComputer, objWMIService, objRefresher, objShell, dtm1, dtm2, speed, stack
  20.   Dim objRefreshableItem1, objPerfNetworkInterface
  21.   strComputer = "."
  22.   Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  23.   Set objRefresher = CreateObject("WbemScripting.SWbemRefresher")
  24.   Set objShell = CreateObject("WScript.Shell")
  25.   ' 先设置standby-timeout = 0
  26.   objShell.Run "powercfg.exe /change standby-timeout-ac 0", 0, True
  27.   objShell.Run "powercfg.exe /change standby-timeout-dc 0", 0, True
  28.   ' addrefresh
  29.   dtm1 = Now
  30.   stack = 0
  31.   Set objRefreshableItem1 = objRefresher.AddEnum(objWMIService, "Win32_PerfFormattedData_Tcpip_NetworkInterface")
  32.   
  33.   Do
  34.     objRefresher.Refresh
  35.     speed = 0
  36.     If objRefreshableItem1.IsSet Then
  37.       For Each objPerfNetworkInterface In objRefreshableItem1.ObjectSet
  38.         speed = speed + objPerfNetworkInterface.BytesReceivedPersec
  39.       Next
  40.     Else
  41.       speed = speed + objRefreshableItem1.Object.BytesReceivedPersec
  42.     End If
  43.     speed = speed * 8
  44.     If conShowWindow Then
  45.       WScript.Echo "speed = " & FormatBps(speed)
  46.     End If
  47.     If speed > conSpeedMax Then
  48.       stack = 0
  49.     Else
  50.       If stack = 0 Then
  51.         dtm1 = Now
  52.         stack = 1
  53.       End If
  54.       dtm2 = Now
  55.       ' WScript.Echo "min = " & DateDiff("s", dtm1, dtm2)
  56.       If DateDiff("s", dtm1, dtm2) >= conInterval Then ' 超过指定时间,设置睡眠时间3
  57.         objShell.Run "powercfg.exe /change standby-timeout-ac 3", 0, True
  58.         objShell.Run "powercfg.exe /change standby-timeout-dc 3", 0, True
  59.         Exit Do
  60.       End If
  61.     End If
  62.     WScript.Sleep conFrequency
  63.   Loop
  64.   Set objShell = Nothing
  65.   Set objRefresher = Nothing
  66.   Set objWMIService = Nothing
  67. End Sub
  68. Function FormatBps(ByVal bps) ' As String
  69.   On Error Resume Next
  70.   If bps < 1000 Then
  71.     FormatBps = FormatNumber(bps, 0) & "b/s"
  72.   ElseIf bps < 1000000 Then
  73.     FormatBps = FormatNumber(bps / 1000, 0) & "kb/s"
  74.   ElseIf bps < 1000000000 Then
  75.     FormatBps = FormatNumber(bps / 1000000, 0) & "mb/s"
  76.   Else
  77.     FormatBps = FormatNumber(bps / 1000000000, 0) & "gb/s"
  78.   End If
  79. End Function
  80. Sub RunasScriptHost(strWSH)
  81.   ' runas cscript.exe or wscript.exe
  82.   On Error Resume Next
  83.   If IsNull(strWSH) Or Not (StrComp(strWSH,"cscript.exe",vbTextCompare) = 0 Or StrComp(strWSH,"wscript.exe",vbTextCompare) = 0) Then
  84.     Exit Sub
  85.   End If
  86.   Dim fso
  87.   Set fso = CreateObject("Scripting.FileSystemObject")
  88.   If StrComp(fso.GetFileName(WScript.FullName),strWSH,vbTextCompare) <> 0  Then
  89.     Dim str,arg,shell
  90.     Set shell = CreateObject("Shell.Application")
  91.     str = ""
  92.     For Each arg In WScript.Arguments
  93.       str = str & " """ & arg & """"
  94.     Next
  95.     shell.ShellExecute strWSH,"//nologo """ & WScript.ScriptFullName & """ " & str, "", "open", 1
  96.     Set shell = Nothing
  97.     Set fso = Nothing
  98.     WScript.Quit
  99.   Else
  100.     Set fso = Nothing
  101.   End If
  102. End Sub
  103. Sub RunasAdmin()
  104.   On Error Resume Next
  105.   If Not IsVista(".") Then Exit Sub
  106.   Dim wshell
  107.   Set wshell = CreateObject("WScript.Shell")
  108.   wshell.RegRead "HKEY_USERS\S-1-5-19\Environment\TEMP"
  109.   If Err.Number <> 0 Then
  110.     Dim str,arg,shell
  111.     str = ""
  112.     Set shell = CreateObject("Shell.Application")
  113.     For Each arg In WScript.Arguments
  114.       str = str & " """ & arg & """"
  115.     Next
  116.     shell.ShellExecute WScript.FullName,"//nologo """ & WScript.ScriptFullName & """ " & str, "", "runas", 1
  117.     Set shell = Nothing  
  118.     Set wshell = Nothing
  119.     WScript.Quit
  120.   Else
  121.     Set wshell = Nothing
  122.   End If
  123. End Sub
  124. Function IsVista(strComputer)
  125.   On Error Resume Next
  126.   IsVista = False
  127.   Dim objWMIService, colOperationSystems, objOperationSystem
  128.   Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  129.   Set colOperationSystems = objWMIService.ExecQuery("Select Version from Win32_OperatingSystem")
  130.   For Each objOperationSystem In colOperationSystems
  131.     If CInt(Left(objOperationSystem.Version, InStr(1,objOperationSystem.Version,".") - 1)) > 5 Then
  132.       IsVista = True
  133.       Exit For
  134.     End If
  135.   Next
  136.   Set colOperationSystems = Nothing
  137.   Set objWMIService = Nothing
  138. End Function
复制代码
微信:flashercs
QQ:49908356

TOP

回复 4# flashercs

不懂代码呀,以前在百度搜到过vbs监控网卡速度的代码,复制了改一下就能用,现在搜不到了

TOP

可以用performance counter class监控下载速度.
微信:flashercs
QQ:49908356

TOP

求教各位大佬

TOP

自顶,求教各位大佬

TOP

返回列表