在hda应用程序中获取远程服务器正常运行

时间:2013-07-26 17:49:26

标签: javascript jquery vbscript hta

我可以使用以下代码ping远程服务器:

function ping(server) {
 var objPing = GetObject("winmgmts:").Get("Win32_PingStatus.Address='" + server + "'");
  $("#myInfo").html("<div><b>" + server + ":</b></div> \
                     <div>IP: " + objPing.ProtocolAddress + "</div> \
                 <div>Time to resolve: " + objPing.ResponseTime + "ms</div>");
    } 

我正在尝试通过添加以下内容来获得远程服务器的正常运行时间:     var objWMIService = GetObject(“winmgmts:\”+ server +“\ root \ cimv2”)

一旦我开始工作,我将能够查询服务器并获得正常运行时间。在vbs中,代码看起来如下所示,但我需要它在我的hta应用程序中工作而不是作为vbs:

Set objWMIService = GetObject("winmgmts:\\" & server & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery _
("Select * From Win32_PerfFormattedData_PerfOS_System")

For Each objOS in colOperatingSystems
intSystemUptime = Int(objOS.SystemUpTime / 60 / 60)
Wscript.Echo intSystemUptime & " hours"
Next

除此之外,我还在我的hta应用程序中包含脚本,该应用程序在点击时启动putty。我发现该功能的解决方案是逃避退格。该功能的剪辑如下所示:

WshShell.Run('"C:/\Program Files (x86)/\Putty/\putty.exe" -ssh ' + server);

所以我认为逃避可能会有所帮助,但我仍然会收到错误:

var objWMIService = GetObject("winmgmts:/\/\" + server + "/\root/\cimv2")

1 个答案:

答案 0 :(得分:2)

一些快速的谷歌搜索出现了this page,据此,这样的事情应该有效:

var server = "."

var locator = new ActiveXObject("WbemScripting.SWbemLocator");
var svc = locator.ConnectServer(server, "/root/cimv2");
svc.Security_.AuthenticationLevel = 0;
svc.Security_.ImpersonationLevel = 3;

var qry = "SELECT SystemUptime FROM Win32_PerfFormattedData_PerfOS_System";
var data = new Enumerator(svc.ExecQuery(qry));
for (;!data.atEnd();data.moveNext()) {
  var counters = new Enumerator(data.item().Properties_);
  for (;!counters.atEnd();counters.moveNext()) {
    WScript.Echo(counters.item().Value);
  }
}