批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程
[批处理文件精品]批处理版照片整理器[批处理文件精品]纯批处理备份&还原驱动在线第三方下载
返回列表 发帖

[转贴] VBS脚本获取系统运行时间x天x小时x分钟

执行效果:
C:\>cscript //nologo test.vbs

No argument provided.  Running against localhost

localhost has been up for 241 minutes, which comes out to:
0 Days
4 Hours
1 Minutes

代码:
  1. ' ==================================================================================================
  2. ' ==================================================================================================
  3. ' "System Uptime", by Harold "Waldo" Grunenwald (waldo@ge.com, harold.grunenwald@gmail.com)
  4. ' Gives a target system's uptime
  5. ' Version 1.01
  6. ' Changes: Initial Values for Days, Hours, and Minutes.  Changed spacing.
  7. ' Level of Fun Writing This:  High
  8. ' ==================================================================================================
  9. ' Usage:  Run script from the command line with a servername as an argument.  If no argument is
  10. '            provided, script assumes localhost.
  11. '    "cscript system_uptime.vbs servername"
  12. '
  13. ' some code from http://www.microsoft.com/technet/scriptcenter/resources/qanda/aug05/hey0802.mspx
  14. ' ==================================================================================================
  15. ' ==================================================================================================
  16. ' Inital Values
  17. uptimeDays    = 0
  18. uptimeHrs    = 0
  19. uptimeMin    = 0
  20. ' ===Establish target machine(s)===
  21. If Wscript.Arguments.Count = 0 Then
  22.     Wscript.Echo vbCrLf & "No argument provided.  Running against localhost"
  23.     strComputer = "localhost"
  24. Else strComputer = WScript.Arguments(0)
  25. End If
  26. fnUptime(strComputer)
  27. ' ===Really the only way to get the uptime===
  28. Function fnUptime(strComputer)
  29.     Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  30.     Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
  31.     For Each objOS in colOperatingSystems
  32.         dtmBootup = objOS.LastBootUpTime
  33.         dtmLastBootupTime = WMIDateStringToDate(dtmBootup)
  34.         dtmSystemUptime = DateDiff("n", dtmLastBootUpTime, Now)        'uptime in minutes
  35.     Next
  36.     timeConversion(dtmSystemUptime)        'convert to days, hours, & minutes
  37. End Function
  38. ' ===Convert the WMI date string to Minutes===
  39. ' Microsoft date cleanup code, bless 'em for doing it
  40. ' TODO:
  41. ' Fix this function on Win2k machines
  42. ' Type Coercion (on CDate)
  43. ' http://www.microsoft.com/technet/scriptcenter/guide/sas_vbs_eves.mspx?mfr=true
  44. Function WMIDateStringToDate(dtmBootup)
  45.     WMIDateStringToDate = CDate(Mid(dtmBootup, 5, 2) & "/" & _
  46.         Mid(dtmBootup, 7, 2) & "/" & Left(dtmBootup, 4) _
  47.             & " " & Mid (dtmBootup, 9, 2) & ":" & _
  48.                 Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup,13, 2))
  49. End Function
  50. ' ===Convert the time in Minutes to Days, Hours, & Minutes===
  51. Function timeConversion(dtmSystemUptime)
  52. ' Set some variables
  53.     uptimeMin = dtmSystemUptime
  54. ' Convert to hours
  55.     if uptimeMin >= 60 then
  56.         uptimeHrs = Int(uptimeMin / 60)    'convert to integer
  57.         uptimeMin = (uptimeMin mod 60)        'final value for minutes
  58.     end if
  59. ' Convert to Days
  60.     if uptimeHrs >= 24 then
  61.         uptimeDays = Int(uptimeHrs / 24)    'convert to integer
  62.         uptimeHrs = (uptimeHrs mod 24)        'final value for hours
  63.     end if
  64. ' ===Output===
  65.     wscript.echo
  66.     wscript.echo strComputer & " has been up for " & dtmSystemUptime & " minutes, which comes out to:"
  67.     wscript.echo uptimeDays & " Days"
  68.     wscript.echo uptimeHrs & " Hours"
  69.     wscript.echo uptimeMin & " Minutes" & vbCrLf
  70. End Function
复制代码

转自 https://github.com/gwaldo/Uptime-for-Windows/blob/master/uptime.vbs

返回列表