1

我不确定为什么,但我的子功能不起作用。我以为我已经遵循了它应该如何工作,但它只会导致一个错误,声称我的函数未定义。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="x-ua-compatible" content="IE=edge">
<title>Remote Registry</title>
<HTA:APPLICATION
  APPLICATIONNAME="Remote Registry"
  ID="RemReg"
  VERSION="1.0.0.0"
  SCROLL="no"
  SINGLEINSTANCE="yes"
  CONTEXTMENU="no"
  NAVIGABLE="yes"
 SELECTION="no"
/>
<style type="text/css">
body
{
    margin: 0;
    width: 130px;
    height: 180px;
    overflow: hidden;
        font-family: arial;
    font-weight: bold;
    font-size: 12px;
}
</style>
</head>
<SCRIPT LANGUAGE="VBScript">
Sub CheckService
    strComputer = txtBox.value
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    Set colRunningServices = objWMIService.ExecQuery _
        ("Select * from Win32_Service Where Name=""RemoteRegistry""")
    For Each objService in colRunningServices 
    Output = objService.DisplayName  & " is " & objService.State
    Next
End Sub
</script>
<body>
    <input type="text" name="TxTbox" size="30" value=DTP-> Computer to check<br />
    <input id=checkservice type="button" value="Add Button" onClick="CheckService">
    <div id="strComputer"></div>
</body>
</html>

我错过了什么?真的很简单吗?我已经尝试了函数的替代名称,移动到 VBScript 所在的位置。没有任何效果:sRem

4

4 回答 4

1

将您的代码更改为以下代码并使用上面的参考链接进行 WMI 调用,您应该没问题。

<!DOCTYPE html>

<html>

<head>
  <meta http-equiv="x-ua-compatible" content="IE=edge" />
  <title>Remote Registry</title>
  <hta:application
    applicationname="Remote Registry"
    id="RemReg"
    version="1.0.0.0"
    scroll="no"
    singleinstance="yes"
    contextmenu="no"
    navigable="yes"
    selection="no"
  />
  <style type="text/css">
    body
    {
      margin: 0;
      width: 130px;
      height: 180px;
      overflow: hidden;
      font-family: arial;
      font-weight: bold;
      font-size: 12px;
    }
  </style>
</head>

<script type="text/vbscript" id="CheckService">
Sub CheckService()
'
dim strComputer
  '
  strComputer = window.document.getElementById("txtComputer").value
  ' PLACE YOUR CALL TO WMI HERE - (I'm not sure mine is correct!)
  'Set objServices = GetObject( _
    "winmgmts:{impersonationLevel=impersonate," _
    & "authenticationLevel=pktPrivacy}!\\" _
    & strcomputer & "/root/cimv2")
  '
  Set colRunningServices = objWMIService.ExecQuery ("Select * from Win32_Service Where Name=" & chr(34) & "RemoteRegistry" & chr(34))
    if colRunningServices.items.count - 1 > 0 Then
      For Each objService in colRunningServices
        window.document.getElementById("Results").innerText = window.document.getElementById("Results").innerText & objService.DisplayName  & " is " & objService.State & chr(13)
      Next
    else
      window.document.getElementById("Results").innerText = "No running services found at this time!"
    end if
  '
End Sub
</script>

<body>
  <input type="text" id="txtComputer" name="txtComputer" size="30" value="Dtp-"/> Computer to check<br />
  <input id="btnCheckService" name="btnCheckService" type="button" value="Add Button" onclick="CheckService" />
  <div id="Results"></div>
</body>
</html>

更新

我已修改代码以删除 META 标记并为您添加一些示例子例程。

我所做的是将 HTA 保存到我的桌面并从那里通过双击它来执行。

<!DOCTYPE html>
<html>
<head>
  <title>Remote Registry</title>
  <hta:application
    applicationname="Remote Registry"
    id="RemReg"
    version="1.0.0.0"
    scroll="no"
    singleinstance="yes"
    contextmenu="no"
    navigable="yes"
    selection="no"
  />
  <style type="text/css">
    body
    {
      margin: 0;
      width: 130px;
      height: 180px;
      overflow: hidden;
      font-family: arial;
      font-weight: bold;
      font-size: 12px;
    }
  </style>
</head>
<script type="text/vbscript" id="EnumMyServices">
' <!--
Sub EnumMyServices()
  dim WMI, objs, obj
  '
  set WMI = GetObject("WinMgmts:")
  on error resume next
    set objs = WMI.InstancesOf("Win32_Service")
    if err = 0 Then
      if objs.count > 0 then
        window.document.getElementById("Results").innerText = "SERVICES" & chr(13)
        for each obj in objs
          window.document.getElementById("Results").innerText = window.document.getElementById("Results").innerText & obj.Description & chr(13)
        next
      else
        window.document.getElementById("Results").innerText = "SERVICES" & chr(13)
        window.document.getElementById("Results").innerText = "no services found!" & chr(13)
      end if
    else
      window.document.getElementById("Results").innerText = "SERVICES" & chr(13)
      window.document.getElementById("Results").innerText = "An error occurred whilst trying to enum services!" & chr(13)
    end if
    set WMI=nothing
    set objs = nothing
    set obj = nothing
  on error goto 0
end sub
' -->
</script>

<script type="text/vbscript" id="EnumMyPrinters">
' <!--
Sub EnumMyPrinters()
dim WMI, objs, obj
  '
  set WMI = GetObject("WinMgmts:")
  on error resume next
    set objs = WMI.InstancesOf("Win32_Printer")
    if err = 0 Then
      if objs.count > 0 then
        window.document.getElementById("Results").innerText = "PRINTERS" & chr(13)
        for each obj in objs
          window.document.getElementById("Results").innerText = window.document.getElementById("Results").innerText & obj.Description & chr(13)
        next
      else
        window.document.getElementById("Results").innerText = "PRINTERS" & chr(13)
        window.document.getElementById("Results").innerText = "no printers found!" & chr(13)
      end if
    else
      window.document.getElementById("Results").innerText = "PRINTERS" & chr(13)
      window.document.getElementById("Results").innerText = "An error occurred whilst trying to enum printers!" & chr(13)
    end if
    set WMI=nothing
    set objs = nothing
    set obj = nothing
  on error goto 0
end sub
' -->
</script>

<script type="text/vbscript" id="EnumMyProcesses">
' <!--
Sub EnumMyProcesses()
dim WMI, objs, obj
  '
  set WMI = GetObject("WinMgmts:")
  on error resume next
    set objs = WMI.InstancesOf("Win32_Process")
    if err = 0 Then
      if objs.count > 0 then
        window.document.getElementById("Results").innerText = "PROCESSES" & chr(13)
        for each obj in objs
          window.document.getElementById("Results").innerText = window.document.getElementById("Results").innerText & obj.Description & chr(13)
        next
      else
        window.document.getElementById("Results").innerText = "PROCESSES" & chr(13)
        window.document.getElementById("Results").innerText = "no processes found!" & chr(13)
      end if
    else
      window.document.getElementById("Results").innerText = "PROCESSES" & chr(13)
      window.document.getElementById("Results").innerText = "An error occurred whilst trying to enum processes!" & chr(13)
    end if
    set WMI=nothing
    set objs = nothing
    set obj = nothing
  on error goto 0
end sub
' -->
</script>

<script type="text/vbscript" id="EnumMyProcessors">
' <!--
Sub EnumMyProcessors()
dim WMI, objs, obj
  '
  set WMI = GetObject("WinMgmts:")
  on error resume next
    set objs = WMI.InstancesOf("Win32_Processor")
    if err = 0 Then
      if objs.count > 0 then
        window.document.getElementById("Results").innerText = "PROCESSORS" & chr(13)
        for each obj in objs
          window.document.getElementById("Results").innerText = window.document.getElementById("Results").innerText & obj.Description & chr(13)
        next
      else
        window.document.getElementById("Results").innerText = "PROCESSORS" & chr(13)
        window.document.getElementById("Results").innerText = "no processors found!" & chr(13)
      end if
    else
      window.document.getElementById("Results").innerText = "PROCESSORS" & chr(13)
      window.document.getElementById("Results").innerText = "An error occurred whilst trying to enum processors!" & chr(13)
    end if
    set WMI=nothing
    set objs = nothing
    set obj = nothing
  on error goto 0
end sub
' -->
</script>

<body>
  <div id="mycontainer">
    Results:<br />
    <div id="Results"></div>
  </div><br /><br />
  <input id="btnServices" name="btnServices" type="button" value="Services" onclick="EnumMyServices" />
  <input id="btnPrinters" name="btnPrinters" type="button" value="Printers" onclick="EnumMyPrinters" />
  <input id="btnProcesses" name="btnProcesses" type="button" value="Processes" onclick="EnumMyProcesses" />
  <input id="btnProcessors" name="btnProcessors" type="button" value="Processors" onclick="EnumMyProcessors" />
</body>
</html>
于 2013-04-30T13:30:32.753 回答
1

使用括号'()'函数定义如下,它解决了你的问题。

子检查服务()

//你的代码

结束子

于 2016-06-17T08:25:00.060 回答
1

当 HTA 文档模式为 9 或更高版本并且从不带括号的 HTML 代码中调用子例程时,会出现“ [Subroutine name] is undefined ”错误。

在这种特殊情况下,代码onClick="CheckService"应该是:onClick="CheckService()"

请注意,此 HTA 的文档模式是不明确的,因为它被声明为:

<meta http-equiv="x-ua-compatible" content="IE=edge">

这会导致 HTA 中的 HTML 代码使用 MSHTML.dll 提供的最高文档模式呈现。它与边缘浏览器无关

在具有 MSHTML 8 或更低版本的机器上测试此 HTA 的人不会看到错误。在任何具有 MSHTML 9 或更高版本的机器上,都会发生该错误。请注意,MSHTML 9 对应于 2011 年发布的 Internet Explorer 9。因此,到 2013 年,当发布此问题时,大多数机器都会收到发布脚本的错误。

非官方接受的答案通过删除元行消除了错误,导致 HTA 在IE 7模式下运行,在 HTML 代码中调用子例程时不需要括号。在这种情况下,降级 HTA 文档模式可能没有负面影响,但会导致 HTML/CSS 代码产生与预期不同的结果。

有关 HTA 文档模式的更多背景信息

HTA 应针对特定的文档模式,以确保 HTML/CSS 代码始终按预期工作。ie=edge选项应该只被包含不同 MSHTML 版本的多个 HTML 代码部分的专家编码员使用。这现在已经过时了,因为生产中的任何机器都应该有 MSHTML 11,它于 2013 年推出,并且永远不会有任何进一步的升级。(注意:Windows 11 不包含 Internet Explorer,但包含 MSHTML 11,因此 HTA 仍然有效。)通常,HTA 应以 IE=9、IE=10 或 IE=11 为目标。选择这三种文档模式中的一种有一些特定的原因,但这是另一回事。

HTA 通常应以如下代码开头:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" http-equiv="X-UA-Compatible" content="IE=9">
<hta:application id=oHTA>
</head>

如果您的 HTA 没有DOCTYPEX-UA-Compatible声明,它将默认在IE 5模式下运行。如果它只有DOCTYPE声明,它将默认在IE 7模式下运行。但是,如果mshta.exeFEATURE_BROWSER_EMULATION注册表设置,则默认模式将是在该注册表值中设置的任何内容。

如果您的 HTA 有X-UA-Compatible声明,它将在该模式下运行而不管任何注册表设置(假设机器没有旧版本的 MSHTML),因此请务必包含如上所示的行(注意:UTF-8 部分是可选的,但有助于使用特殊字符)。

于 2021-12-15T03:11:57.537 回答
0

服务列表结果的一些基本 HTML 格式使其更易于阅读。

<!DOCTYPE html>
<html>
    <head>
        <title>Remote Registry</title>
        <hta:application
            applicationname="Remote Registry"
            id="RemReg"
            version="1.0.0.0"
            scroll="YES"
            singleinstance="yes"
            contextmenu="no"
            navigable="yes"
            selection="no"
            />
        <style type="text/css">
            body
            {
                margin: 0;
                width: 100%;
                height: 100%;
                overflow: hidden;
                font-family: arial;
                font-weight: bold;
                font-size: 12px;
            }
        </style>
        <script type="text/vbscript" id="EnumMyServices">

            ' <!--
            Sub EnumMyServices()
            dim WMI, objs, obj
            '
            set WMI = GetObject("WinMgmts:")
            on error resume next
            set objs = WMI.InstancesOf("Win32_Service")
            if err = 0 Then
            if objs.count > 0 then
            strHTML="<table border=1 align=left cellpadding=4 cellspacing=0>"
            strHTML=strHTML &  "<tr>"
            strHTML=strHTML &  "<th>DisplayName</th>"
            strHTML=strHTML &  "<th>State</th>"
            strHTML=strHTML &  "<th>StartMode</th>"
            strHTML=strHTML &  "</tr>"
            for each obj in objs
            ObName = obj.DisplayName 
            ObState = obj.State 
            ObStartMode = obj.StartMode 
            strHTML=strHTML &  "<tr>"
            strHTML=strHTML &  "<td>" & ObName & "</td>"
            strHTML=strHTML &  "<td>" & ObState & "</td>"
            strHTML=strHTML &  "<td>" & ObStartMode & "</td>"
            strHTML=strHTML &  "</tr>"
            next
            else
            strHTML="<table border=1 align=left cellpadding=4 cellspacing=0>"
            strHTML=strHTML &  "<tr>"
            strHTML=strHTML &  "<th>Attention</th>"
            strHTML=strHTML &  "</tr>"
            strHTML=strHTML &  "<tr>"
            strHTML=strHTML &  "<td>no services found!</td>"
            strHTML=strHTML &  "</tr>"
            end if
            else
            strHTML="<table border=1 align=left cellpadding=4 cellspacing=0>"
            strHTML=strHTML &  "<tr>"
            strHTML=strHTML &  "<th>Error!</th>"
            strHTML=strHTML &  "</tr>"
            strHTML=strHTML &  "<tr>"
            strHTML=strHTML &  "<td>An error occurred whilst trying to enum services!</td>"
            strHTML=strHTML &  "</tr>"
            end if
            set WMI=nothing
            set objs = nothing
            set obj = nothing
            on error goto 0

            strHTML=strHTML &  "</table>"
            strHTML=strHTML &  "<p>"   
            Results.InnerHTML=strHTML

            end sub
            ' -->
        </script>

        <script type="text/vbscript" id="EnumMyPrinters">
            ' <!--
            Sub EnumMyPrinters()
            dim WMI, objs, obj
            '
            set WMI = GetObject("WinMgmts:")
            on error resume next
            set objs = WMI.InstancesOf("Win32_Printer")
            if err = 0 Then
            if objs.count > 0 then
            window.document.getElementById("Results").innerText = "PRINTERS" & chr(13)
            for each obj in objs
            window.document.getElementById("Results").innerText = window.document.getElementById("Results").innerText & obj.Description & chr(13)
            next
            else
            window.document.getElementById("Results").innerText = "PRINTERS" & chr(13)
            window.document.getElementById("Results").innerText = "no printers found!" & chr(13)
            end if
            else
            window.document.getElementById("Results").innerText = "PRINTERS" & chr(13)
            window.document.getElementById("Results").innerText = "An error occurred whilst trying to enum printers!" & chr(13)
            end if
            set WMI=nothing
            set objs = nothing
            set obj = nothing
            on error goto 0
            end sub
            ' -->
        </script>

        <script type="text/vbscript" id="EnumMyProcesses">
            ' <!--
            Sub EnumMyProcesses()
            dim WMI, objs, obj
            '
            set WMI = GetObject("WinMgmts:")
            on error resume next
            set objs = WMI.InstancesOf("Win32_Process")
            if err = 0 Then
            if objs.count > 0 then
            window.document.getElementById("Results").innerText = "PROCESSES" & chr(13)
            for each obj in objs
            window.document.getElementById("Results").innerText = window.document.getElementById("Results").innerText & obj.Description & chr(13)
            next
            else
            window.document.getElementById("Results").innerText = "PROCESSES" & chr(13)
            window.document.getElementById("Results").innerText = "no processes found!" & chr(13)
            end if
            else
            window.document.getElementById("Results").innerText = "PROCESSES" & chr(13)
            window.document.getElementById("Results").innerText = "An error occurred whilst trying to enum processes!" & chr(13)
            end if
            set WMI=nothing
            set objs = nothing
            set obj = nothing
            on error goto 0
            end sub
            ' -->
        </script>

        <script type="text/vbscript" id="EnumMyProcessors">
            ' <!--
            Sub EnumMyProcessors()
            dim WMI, objs, obj
            '
            set WMI = GetObject("WinMgmts:")
            on error resume next
            set objs = WMI.InstancesOf("Win32_Processor")
            if err = 0 Then
            if objs.count > 0 then
            window.document.getElementById("Results").innerText = "PROCESSORS" & chr(13)
            for each obj in objs
            window.document.getElementById("Results").innerText = window.document.getElementById("Results").innerText & obj.Description & chr(13)
            next
            else
            window.document.getElementById("Results").innerText = "PROCESSORS" & chr(13)
            window.document.getElementById("Results").innerText = "no processors found!" & chr(13)
            end if
            else
            window.document.getElementById("Results").innerText = "PROCESSORS" & chr(13)
            window.document.getElementById("Results").innerText = "An error occurred whilst trying to enum processors!" & chr(13)
            end if
            set WMI=nothing
            set objs = nothing
            set obj = nothing
            on error goto 0
            end sub
            ' -->
        </script>
    </head>

    <body>
        <input id="btnServices" name="btnServices" type="button" value="Services" onclick="EnumMyServices" />
        <input id="btnPrinters" name="btnPrinters" type="button" value="Printers" onclick="EnumMyPrinters" />
        <input id="btnProcesses" name="btnProcesses" type="button" value="Processes" onclick="EnumMyProcesses" />
        <input id="btnProcessors" name="btnProcessors" type="button" value="Processors" onclick="EnumMyProcessors" />
        <p>
        <Font size="3" face="Tahoma"><h3>Results:</Font><hr></h3>
        <div id="Results">&nbsp</div>

    </body>
</html>
于 2013-11-07T20:51:18.557 回答