home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2007 April / PCpro_2007_04.ISO / files / usb / VMware-server-installer101.exe / sample2.vbs < prev    next >
Encoding:
Text File  |  2006-08-09  |  5.0 KB  |  132 lines

  1. '  VmCOM VBScript Sample Script (sample2)
  2. '  Copyright 1998 VMware, Inc.  All rights reserved. -- VMware Confidential
  3. '  
  4. '  Permission is hereby granted, free of charge, to any person obtaining a
  5. '  copy of the software in this file (the "Software"), to deal in the
  6. '  Software without restriction, including without limitation the rights to
  7. '  use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. '  copies of the Software, and to permit persons to whom the Software is
  9. '  furnished to do so, subject to the following conditions:
  10. '  
  11. '  The above copyright notice and this permission notice shall be included in
  12. '  all copies or substantial portions of the Software.
  13. '  
  14. '  The names "VMware" and "VMware, Inc." must not be used to endorse or
  15. '  promote products derived from the Software without the prior written
  16. '  permission of VMware, Inc.
  17. '  
  18. '  Products derived from the Software may not be called "VMware", nor may
  19. '  "VMware" appear in their name, without the prior written permission of
  20. '  VMware, Inc.
  21. '  
  22. '  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  23. '  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  24. '  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  25. '  VMWARE,INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  26. '  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. '  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. '
  29. '  ------
  30. '
  31. '  This program is for educational purposes only.
  32. '  It is not to be used in production environments.
  33. '
  34. '  Description:
  35. '
  36. '  This script displays the virtual machines on the local server.
  37. '  It prints the configuration file path and current execution
  38. '  state of each VM. If a VM is in the stuck state, the current
  39. '  question and its choices are also printed.
  40. '  Additionally, if a VM is stuck on an undoable disk related
  41. '  question, the script automatically answers 'Keep' on a power-off
  42. '  and 'Append' on a power-on.
  43. '
  44. '  NOTE: the question-answering logic used is language and product
  45. '        dependent, and is only provided for illustration purposes only!
  46. '
  47. '  Instructions for Windows 2000 and later operating systems:
  48. '
  49. '  - save the contents of this file to a file named 'sample2.vbs'
  50. '    unless it's already named that way
  51. '
  52. '  - there should be an accompanying file named 'sample2.wsf'
  53. '    It is placed in the same directory as this file during
  54. '    product installation. This file is responsible for setting
  55. '    up the Windows Script Host environment and loading the
  56. '    VmCOM type library, thereby enabling this script to 
  57. '    reference symbolic constants such as vmExecutionState_On
  58. '
  59. '  - in a command line window, type:
  60. '    cscript //nologo sample2.wsf
  61.  
  62.  
  63. Set cp = CreateObject("VmCOM.VmConnectParams")
  64. Set server = CreateObject("VmCOM.VmServerCtl")
  65.  
  66. server.Connect cp 
  67. Set vmCollection = server.RegisteredVmNames
  68.  
  69. for each vmName in vmCollection
  70.    Set vm = CreateObject("VmCOM.VmCtl")
  71.    s = "path=" & vmName
  72.    On error resume next  ' Clear error object
  73.    vm.Connect cp,vmName
  74.    if err.Number = vmErr_VMBUSY then
  75.       s = s & " UNAVAILABLE (controlled by local console)"
  76.    elseif err.Number <> 0 then
  77.       s = s & " ERROR CONNECTING desc='" & err.Description & "'"
  78.    else
  79.       On error goto 0  'Make errors fatal past this point
  80.       s = s & " state=" & State2Str(vm) & " os=" & vm.Config("guestos") 
  81.       if vm.ExecutionState = vmExecutionState_Stuck then
  82.          Set q = vm.PendingQuestion
  83.          Set choices = q.choices
  84.          s = s & " question= '" & q.text & "' choices="
  85.          for each choice in choices
  86.          s = s & "[" & choice & "] "
  87.          next
  88.    
  89.          ' If this looks like an undoable disk save question,
  90.          ' automatically answer 'Append' or 'Keep'
  91.          '
  92.          ' NOTE: this code makes alot of assumptions about the product
  93.          '       and the language used, and may break under some environments.
  94.          '       It is shown for illustration purposes only!
  95.  
  96.          Set r = new RegExp     
  97.          r.pattern = "undoable disk"
  98.          r.ignorecase = True
  99.          Set matches = r.Execute(q.text)
  100.  
  101.          if matches.count > 0 then
  102.             for i = 1 to choices.count
  103.            if choices(i) = "Append" or choices(i) = "Keep" then
  104.                   WScript.Echo(s)
  105.                   s = "   --> Automatically selecting '" & q.choices(i) & "' as answer"
  106.                   vm.AnswerQuestion q,i
  107.               exit for
  108.                end if
  109.             next
  110.          end if 
  111.       end if
  112.    end if
  113.    WScript.Echo(s)
  114. next
  115.  
  116. function State2Str(vm)
  117.    select case vm.ExecutionState
  118.       case vmExecutionState_On
  119.          State2Str = "ON"
  120.       case vmExecutionState_Off
  121.          State2Str = "OFF"
  122.       case vmExecutionState_Suspended
  123.          State2Str = "SUSPENDED"
  124.       case vmExecutionState_Stuck
  125.          State2Str = "STUCK"
  126.       case else
  127.          State2Str = "UNKNOWN"
  128.    end select
  129. end function
  130.