子功能在HTA中不起作用

时间:2013-04-30 12:43:08

标签: html vbscript hta

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

<!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

3 个答案:

答案 0 :(得分:0)

将您的代码更改为以下内容并使用上面的参考链接进行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>

答案 1 :(得分: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>

答案 2 :(得分:0)

使用括号&#39;()&#39;功能定义如下,它解决了你的问题。

Sub CheckService()

//您的代码

End Sub