简体   繁体   English

将UpTime JS代码转换为VBS

[英]Convert UpTime JS code to VBS

I'm generating an "Up-Time" Win7 Gadget and trying to reproduce the .vbs code found in similar Gadgets. 我正在生成“正常运行时间” Win7小工具,并尝试重现在类似小工具中找到的.vbs代码。

I'm a .js coder. 我是.js编码器。

Relevant JS: 相关JS:

vbStr=GetUpTime();

Relevant VBS: 相关的VBS:

Function GetUpTime
    Set loc=CreateObject("WbemScripting.SWbemLocator")
    Set svc=loc.ConnectServer(MachineName, "root\cimv2")
    Set oss=svc.ExecQuery("SELECT * FROM Win32_OperatingSystem")
    For Each os in oss
        tim=os.LastBootUpTime
    Next
    GetUpTime=tim
End Function

Essentially this .vbs does the trick, as currently there is only 1 os running. 本质上,这个.vbs可以解决问题,因为目前只有1个os正在运行。 I would like to expand on this by learning: 我想通过学习来扩展这一点:

1) What is the relevance of MachineName ? 1) MachineName的意义是什么?

If I return MachineName instead of tim , I get an undefined value. 如果返回MachineName而不是tim ,则会得到undefined值。

2) How to extract individual os 's without the For Each loop, equivelant to the .js: 2)如何在不使用For Each循环的情况下提取与js等价的单个os

os=oss[n];

3) How to return an array of tim 's relative to each os . 3)如何返回相对于每个ostim数组。

The .vbs code loops through the available os 's and gets their respective up-times, but the developer only planned for 1 os and as such there was no code to return an array of tim 's. .vbs代码遍历可用的os并获得各自的正常运行时间,但是开发人员仅计划了1 os ,因此没有代码可返回tim的数组。 After researching .vbs arrays I've found how to create a 'set-length' array, but this is not relevant! 研究了.vbs数组后,我发现了如何创建“定长”数组,但这无关紧要!

  1. Machine name is undefined so treated as a zero length string. 机器名未定义,因此被视为零长度的字符串。 Which means it's ignored. 这意味着它被忽略了。 Normally it's the computer on the network that you wish to query. 通常,您要查询的是网络上的计算机。 It's optional so it's undeclaredness doesn't raise an error. 它是可选的,因此它的未声明性不会引发错误。

  2. Using COM abbreviation (where Items is a default property) 使用COM缩写(其中Items是默认属性)

      os=oss(1) 

or in full 或全部

      os=oss.items(1)
  1. A dictionary is easier (from Help). 字典更容易(来自“帮助”)。

      Set d = CreateObject("Scripting.Dictionary") d.Add "a", "Athens" ' Add some keys and items. d.Add "b", "Belgrade" d.Add "c", "Cairo" 

NB: JScript uses COM just like VBScript. 注意:JScript与VBScript一样使用COM。 The code would be similar. 该代码将是相似的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM