Board logo

标题: 微软vbs脚本工具 代码 [打印本页]

作者: myzam    时间: 2011-2-22 21:04     标题: 微软vbs脚本工具 代码

本代码是从微软转载的,把文件保存格式为hta.  就可以方便的在vbs中引用wmi对象了。wmi对象,简单的讲就是dos的继承人,尤其善长于管理电脑。要管理电脑一味的只用老dos会力不从心的。wimc 命令就是dos命令。
如要查看本机的 进程,并用逗点分隔值csv(comma separate value) 显示在cmd中就可以这样:
wmic  process list brief  /format:csv.xsl
或 wimc process
或 wimc process list
自由而规范 这是wmic的特点,毕竟它是诞生于现代,dos有点古老,语言不太规范了。

下面的hta文件(html applocation)其实就是html文件。
html文件的宿主(就是html文件的运行环境,如dos的运行环境就是有cmd创立)是IE
HTa的运行环境就是mshta(位于你的系统文件夹里 你可以到systme32 中去插看。)创立的。它就是宿主。
vbs的宿主就是wscript。


  1. <html>
  2. <!--********************************************************************
  3. '*
  4. '*  File:           scriptomatic.hta
  5. '*  Created:        August 2002
  6. '*  Version:        1.0
  7. '*
  8. '*  Description:    Learning tool. Enables users to generate and run
  9. '*                  WSH scripts (in VBScript) that use WMI to display
  10. '*                  properties available through the Win32_ classes.      
  11. '*
  12. '*
  13. '* Copyright (C) 2002 Microsoft Corporation
  14. '*
  15. '********************************************************************-->
  16. <title>Windows .NET Server Resource Kit - Scriptomatic</title>
  17. <HTA:APPLICATION
  18.      ID="objScriptomatic"
  19.      APPLICATIONNAME="Scriptomatic"
  20.      SCROLL="no"
  21.      SINGLEINSTANCE="yes"
  22.      WINDOWSTATE="normal"
  23. >
  24. <head>
  25. <style>
  26. BODY
  27. {
  28.    background-color: buttonface;
  29.    font-family: Helvetica;
  30.    font-size: 8pt;
  31.    margin-top: 10px;
  32.    margin-left: 10px;
  33.    margin-right: 10px;
  34.    margin-bottom: 10px;
  35. }
  36. .button
  37. {
  38.    font-family: Helvetica;
  39.    font-size: 8pt;
  40.    width: 35px;
  41. }
  42. textarea
  43. {
  44.    font-family: arial;
  45.    font-size: 8pt;
  46.    margin-left: 3px;
  47. }
  48. select
  49. {
  50.    font-family: arial;
  51.    font-size: 8pt;
  52.    width: 450px;
  53.    margin-left: 0px;
  54. }
  55. </style>
  56. <script language="vbscript">
  57. '*********************************************
  58. '* WHILE LOADING...
  59. '*
  60. '* As the application loads, we open a new
  61. '* browser window to act as a crude progress dialog
  62. '* while we wait for the enumeration of the WMI
  63. '* classes to complete.
  64. '*
  65. '* We minimize the parent window prior to presenting
  66. '* the progress dialog and resize it back to normal
  67. '* once the classes are enumerated.
  68. '*
  69. '*********************************************
  70. Sub Window_Onload
  71.    '* resize parent window
  72.    self.ResizeTo 1,1
  73.    self.MoveTo 300,300
  74.    
  75.    '* create dialog window
  76.    Set objDialogWindow = window.Open("about:blank","rogressWindow","height=15,width=250,left=300,top=300,status=no,titlebar=no,toolbar=no,menubar=no,location=no,scrollbars=no")
  77.    objDialogWindow.Focus()
  78.    objDialogWindow.ResizeTo 250,15
  79.    objDialogWindow.document.body.style.fontFamily = "Helvetica"
  80.    objDialogWindow.document.body.style.fontSize = "11pt"
  81.    objDialogWindow.document.writeln "<html><body>Loading WMI Classes.</body></html>"
  82.    objDialogWindow.document.title = "lease wait."
  83.    objDialogWindow.document.body.style.backgroundColor = "buttonface"
  84.    objDialogWindow.document.body.style.borderStyle = "none"
  85.    objDialogWindow.document.body.style.marginTop = 15
  86.    '****************************************************************************
  87.    '* enumerate the WMI classes in the cimv2 namespace, filling up a recordset
  88.    '* with the names of the classes that begin with Win32_ and are not association
  89.    '* classes. we'll use the class names stored in the recordset to populate a
  90.    '* pulldown.
  91.    '*****************************************************************************
  92.    Const adVarChar = 200
  93.    Const MaxCharacters = 255
  94.    strComputer = "."
  95.    
  96.    Set rsDataList = CreateObject("ADODB.Recordset")
  97.    rsDataList.Fields.Append "ClassName", adVarChar, MaxCharacters
  98.    rsDataList.Open
  99.    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  100.    
  101.    iCounter = 0
  102.    For Each objclass in objWMIService.SubclassesOf()
  103.    '* write a . to the dialog window once for every 250 classes
  104.    '* to let the user know something is still happening.
  105.       iCounter = iCounter + 1
  106.       If iCounter Mod 250 = 0 Then
  107.          objDialogWindow.document.writeln "."
  108.       End If
  109.       bIsQualifier = False
  110.       If UCase(Left(objClass.Path_.Class,5)) = "WIN32" Then
  111.          For Each Qualifier in objClass.Qualifiers_
  112.             If UCase(Trim(Qualifier.Name)) = "ASSOCIATION" Then
  113.                bIsQualifier = True
  114.             End If
  115.          Next
  116.    '* the class name starts with win32_ and is not an association
  117.    '* class - so append it to the recordset
  118.          If bIsQualifier = False Then
  119.             rsDataList.AddNew
  120.             rsDataList("ClassName") = objClass.Path_.Class
  121.             rsDataList.Update
  122.          End If
  123.       End If
  124.    Next
  125.    '* populate the pulldown
  126.    rsDataList.Sort = "ClassName"
  127.    rsDataList.MoveFirst
  128.    strHTML = "<select onChange=""ComposeCode()"" name=ClassesPulldown>" &_
  129.              "<option value=""ulldownMessage"">Begin by selecting a class"
  130.    Do Until rsDataList.EOF
  131.       strHTML = strHTML & "<option value= " & chr(34) &_
  132.       rsDataList.Fields.Item("ClassName") & chr(34) &_
  133.       ">" & rsDataList.Fields.Item("ClassName")
  134.       rsDataList.MoveNext
  135.    Loop
  136.    strHTML = strHTML & "</select>"
  137.    wmi_classes.insertAdjacentHTML "beforeEnd", strHTML
  138.    '* the classes are enumerated, close the progress dialog
  139.    '* and resize the main window
  140.    objDialogWindow.Close
  141.    self.Focus()
  142.    self.ResizeTo 670,550
  143.    self.MoveTo 200,200
  144.    
  145.    '* the user hasn't had a chance to select a class and generate
  146.    '* a script - so disable the run and save buttons because
  147.    '* they are not yet meaningful.
  148.    
  149.    run_button.disabled = True
  150.    save_button.disabled = True
  151. End Sub
  152. '****************************************************************************
  153. '* when the user selects a class from the pulldown, the ComposeCode subroutine
  154. '* is called. it queries WMI to determine the properties of the class the user
  155. '* selected and uses the information to construct sample code which it puts
  156. '* in the main window's textarea.
  157. '****************************************************************************
  158. Sub ComposeCode
  159.    '* if the user happens to select the message instead of a class, just
  160.    '* disable the run and save buttons and exit the subroutine
  161.    If ClassesPulldown.Value = "ulldownMessage" Then
  162.       run_button.disabled = True
  163.       save_button.disabled = True
  164.       Exit Sub
  165.    End If
  166.    strComputer = "."
  167.    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  168.    Set objClass = objWMIService.Get(ClassesPulldown.Value)
  169.    strHTML = "<textarea cols=100 rows=30>"
  170.    strHTML = strHTML & "On Error Resume Next" & Chr(10)
  171.    strHTML = strHTML & "strComputer = " & chr(34) & "." & chr(34) & Chr(10)
  172.    strHTML = strHTML & "Set objWMIService = GetObject(" & chr(34) & "winmgmts:\\" & chr(34) & " & strComputer & " & chr(34) & "\root\cimv2" & chr(34) & ")" & Chr(10)
  173.    strHTML = strHTML & "Set colItems = objWMIService.ExecQuery(" & chr(34) & "Select * from " & ClassesPulldown.value & chr(34) & ",,48)" & Chr(10)
  174.    strHTML = strHTML & "For Each objItem in colItems" & Chr(10)
  175.    For Each objProperty in objClass.properties_
  176.       strHTML = strHTML & "    Wscript.Echo " & chr(34) & objProperty.name & ": " & chr(34) & " & " & "objItem." & objProperty.name & Chr(10)
  177.    Next
  178.    strHTML = strHTML & "Next" & "</textarea>"
  179.    code.InnerHTML= strHTML
  180.    '* once the code is successfully composed and put into the textarea, ensure
  181.    '* that the run and save buttons are enabled
  182.    run_button.disabled = False
  183.    save_button.disabled = False
  184. End Sub
  185. '****************************************************************************
  186. '* when the user presses the Run button, we use the WshShell object's Run
  187. '* method to run the code currently in the textarea under cscript.exe. we use
  188. '* cmd.exe's /k parameter to ensure the command window remains visible after
  189. '* the script has finished running.
  190. '****************************************************************************
  191. Sub RunScript
  192.    Set objFS = CreateObject("Scripting.FileSystemObject")
  193.    strTmpName = "temp_script.vbs"
  194.    Set objScript = objFS.CreateTextFile(strTmpName)
  195.    objScript.Write code.InnerText
  196.    objScript.Close
  197.    Set objShell = CreateObject("WScript.Shell")
  198.    strCmdLine = "cmd /k cscript.exe "
  199.    strCmdLine = strCmdLine & strTmpName
  200.    objShell.Run(strCmdLine)
  201. End Sub
  202. '****************************************************************************
  203. '* when the user presses the Save button, we present them with an InputBox
  204. '* and force them to give us the full path to where they'd like to the save
  205. '* the script that is currently in the textarea. The user is probably quite
  206. '* upset with our laziness here....and who can blame them?
  207. '****************************************************************************
  208. Sub SaveScript
  209.    Set objFSO = CreateObject("Scripting.FileSystemObject")
  210.    strSaveFileName = InputBox("lease enter the complete path where you want to save your script (for example, C:\Scripts\MyScript.vbs).")
  211.    If strSaveFileName = "" Then
  212.       Exit Sub
  213.    End If
  214.    Set objFile = objFSO.CreateTextFile(strSaveFileName)
  215.    objFile.WriteLine code.InnerText
  216.    objFile.Close
  217. End Sub
  218. '****************************************************************************
  219. '* when the user presses the Open button, we present them with an InputBox
  220. '* and force them to give us the full path to the script they'd like to open.
  221. '* This is, of course, rather wonky - but it's meant to be.
  222. '****************************************************************************
  223. Sub OpenScript
  224.    Set objFSO = CreateObject("Scripting.FileSystemObject")
  225.    strOpenFileName = InputBox("lease enter the complete path name for your script (for example, C:\Scripts\MyScript.vbs).")
  226.    If strOpenFileName = "" Then
  227.       Exit Sub
  228.    End If
  229.    Set objFile = objFSO.OpenTextFile(strOpenFileName)
  230.    strHTML = "<textarea cols=100 rows=30>"
  231.    strHTML = strHTML & objFile.ReadAll()
  232.    strHTML = strHTML & "</textarea>"
  233.    code.InnerHTML =  strHTML
  234.    objFile.Close
  235.    run_button.disabled = False
  236.    save_button.disabled = False
  237. End Sub
  238. '****************************************************************************
  239. '* when the user presses the Quit button, the file where we've been storing
  240. '* the scripts gets deleted and the main window closes.
  241. '****************************************************************************
  242. Sub QuitScript
  243.    On Error Resume Next
  244.    Set objFSO = CreateObject("Scripting.FileSystemObject")
  245.    objFSO.DeleteFile "temp_script.vbs"
  246.    Set objFSO = Nothing
  247.    self.Close()
  248. End Sub
  249. </script>
  250. </head>
  251. '***********************************************************
  252. '* our HTML layout - the only thing of note here is that when
  253. '* each of the buttons is pressed (clicked), their onClick
  254. '* attributes causes the appropriate subroutine to be called
  255. '***********************************************************
  256. <body>
  257. <table>
  258.    <td>
  259.       <span id="wmi_classes"> </span>
  260.       <input id=runbutton  class="button" type="button" value="Run"  name="run_button"  onClick="RunScript()">
  261.       <input id=savebutton class="button" type="button" value="Save" name="save_button" onClick="SaveScript()">
  262.       <input id=openbutton class="button" type="button" value="Open" name="open_button" onClick="OpenScript()">
  263.       <input id=quitbutton class="button" type="button" value="Quit" name="quit_button" onClick="QuitScript()">
  264.       <div ID=code_header></div>
  265.       <div id="code"></div>
  266.    </td>
  267. </tr>
  268. </table>
  269. </body>
  270. </html>
复制代码

[ 本帖最后由 myzam 于 2011-2-26 22:51 编辑 ]




欢迎光临 批处理之家 (http://bbs.bathome.net/) Powered by Discuz! 7.2