Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 36.54 KB | None | 0 0
  1. ' VMware support script, VBscript version
  2. '  Collects various configuration and log files
  3. '  for use when troubleshooting the VMware Workstation
  4. '  or the VMware GSX Server
  5.  
  6. Option Explicit
  7. ' On Error Resume Next
  8.  
  9. Const HKLM = &H80000002
  10. Const COMMON_APPDATA = &H23&
  11. Const USER_APPDATA = &H1A&
  12. Const PROGRAM_FILES = &H26&
  13. Const WINDOWS_DIR = &H24&
  14.  
  15. ' Cannot split the string onto another line. If split, it results in a
  16. ' compilation error. Hence the misalignment.
  17. Const ONE_INSTANCE_ONLY = _
  18. "Please make sure only one instance of vm-support.vbs is running at a time."
  19.  
  20. Dim quietMode, outputFolder, collectUILogs, collectLogsForVMs
  21.  
  22. ' Convert and quote a string
  23. Function Quote(strin)
  24.     Dim siz, i, s
  25.     siz = Len(strin)
  26.     For i=1 to siz
  27.         s = s & Chr(Asc(Mid(strin, i, 1)))
  28.     Next
  29.     Quote = Chr(34) & s & Chr(34)
  30. End Function
  31.  
  32. Function NeedQuote(strin)
  33.     Dim siz, i
  34.     siz = Len(strin)
  35.     For i=1 to siz
  36.         If Asc(Mid(strin, i, 1)) = 32 Then
  37.             NeedQuote = True
  38.             Exit Function
  39.         End If
  40.     Next
  41.     NeedQuote = False
  42. End Function
  43.  
  44. Function FormatArguments
  45.     Dim s, arg
  46.     s = ""
  47.     For Each arg In Wscript.Arguments
  48.         If NeedQuote(arg) Then
  49.             s = s & " " & Quote(arg)
  50.         Else
  51.             s = s & " " & arg
  52.         End If
  53.     Next
  54.     FormatArguments = s
  55. End Function
  56.  
  57. Class VMsupport
  58.  
  59.     Private tmpdir, vmtmpdir, workdir
  60.     Private AppData, UserData, SysTemp, Username, ProgFiles, Minidump, SysTemp_VmwareSys, AllUsersProfile
  61.     Private versionInfo, majorVersion, minorVersion
  62.     Private archVersion
  63.     Private osVersion
  64.     Private WindowsDir
  65.     Private Fso, Wsh, RegObj
  66.     Private zipExe
  67.     Private CollectStateLogs
  68.     Private VMList
  69.  
  70.     Private Sub Class_Initialize()
  71.         Dim sh, desktop, objShell, wshNetwork, objWMI, colItems, objItem
  72.  
  73.     On Error Resume Next
  74.  
  75.         Set RegObj=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
  76.                     & ".\root\default:StdRegProv")
  77.        Set objWMI = GetObject("winmgmts:" _
  78.             & "{impersonationLevel=impersonate}!\\.\root\cimv2")
  79.        Set colItems = objWMI.ExecQuery("SELECT * FROM Win32_OperatingSystem",,48)
  80.  
  81.        'Getting the version number for the OS (could be used later)
  82.        For Each objItem in colItems
  83.        osVersion=objItem.Version
  84.        Next
  85.        'Spliting the OS version into major minor and service pack if needed in future
  86.        versionInfo = Split(osVersion, ".")
  87.        majorVersion = CInt(versionInfo(0))
  88.        minorVersion = CInt(versionInfo(1))
  89.        'AddressWidth parameter in Win32_Processor class query
  90.        'for finding if OS is 64bit or 32bit
  91.        archVersion = GetObject("winmgmts:root\cimv2:" & _
  92.                         "Win32_Processor='cpu0'").AddressWidth
  93.        Set Fso = CreateObject("Scripting.FileSystemObject")
  94.  
  95.        set Wsh = WScript.CreateObject("WScript.Shell")
  96.        set wshNetwork = CreateObject("WScript.Network")
  97.        Username = wshNetwork.Username
  98.        desktop = Wsh.SpecialFolders("Desktop")
  99.        tmpdir = Wsh.Environment("Process").Item("Temp")
  100.        SysTemp = Wsh.Environment("Process").Item("WINDIR") & "\Temp"
  101.        SysTemp_VmwareSys = SysTemp & "\vmware-system"
  102.        Minidump = Wsh.Environment("Process").Item("WINDIR") & "\Minidump"
  103.        AllUsersProfile = Wsh.Environment("Process").Item("ALLUSERSPROFILE")
  104.        If outputFolder = "" Then
  105.            vmtmpdir = tmpdir & "\vmware-support"
  106.        Else
  107.            vmtmpdir = outputFolder
  108.        End If
  109.        if not Fso.FolderExists(vmtmpdir) then
  110.            Fso.CreateFolder(vmtmpdir)
  111.        end if
  112.        workdir = vmtmpdir & "\vmsupport-" & Month(Date) _
  113.                         & "-" & Day(Date) & "-" & Year(Date) & "-" _
  114.                         & Hour(Now) & "-" & Minute(Now)
  115.        If Fso.FolderExists(workdir) Then
  116.            Fso.DeleteFolder workdir, True
  117.            If Err.number <> 0 Then
  118.               If Not quietMode Then
  119.                  WriteLine ""
  120.                  WriteLine " Could not delete folder " & workdir &". " & _
  121.                     ONE_INSTANCE_ONLY
  122.               End If
  123.               WScript.Quit(0)
  124.            End If
  125.        End If
  126.        Set VMList = CreateObject("Scripting.Dictionary")
  127.        If Err.number <> 0 Then
  128.           If Not quietMode Then
  129.              WriteLine ""
  130.              WriteLine " Could not create the scripting dictionary. " & _
  131.                        ONE_INSTANCE_ONLY
  132.           End If
  133.           WScript.Quit(0)
  134.        End If
  135.        Fso.CreateFolder(workdir)
  136.        Fso.CreateFolder(workdir & "\Misc")
  137.        Fso.CreateFolder(workdir & "\Dumps")
  138.        Fso.CreateFolder(workdir & "\VM")
  139.        Fso.CreateFolder(workdir & "\TEMP")
  140.        Fso.CreateFolder(workdir & "\Global_Config")
  141.        Fso.CreateFolder(workdir & "\AceServer")
  142.        Fso.CreateFolder(workdir & "\SYSTEMP")
  143.        Fso.CreateFolder(workdir & "\SYSTEMP\vmware-system")
  144.        Fso.CreateFolder(workdir & "\MiniDump")
  145.        Fso.CreateFolder(workdir & "\DxDiag")
  146.        Set objShell = CreateObject("Shell.Application")
  147.        AppData = objShell.Namespace(COMMON_APPDATA).Self.Path
  148.        UserData = objShell.Namespace(USER_APPDATA).Self.Path
  149.        WindowsDir = objShell.Namespace(WINDOWS_DIR).Self.Path
  150.        ProgFiles = objShell.Namespace(PROGRAM_FILES).Self.Path
  151.        zipExe = """" & Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - _
  152.        Len(WScript.ScriptName)) & "zip.exe"""
  153.    End Sub
  154.  
  155.    Sub DumpKey(DefKey, Path, filename)
  156.        Dim f1
  157.  
  158.     On Error Resume Next
  159.  
  160.        Set f1 = fso.CreateTextFile(filename, True, True)
  161.     If Err.number <> 0 Then
  162.        If Not quietMode Then
  163.           WriteLine ""
  164.           WriteLine "Could not create text file " & filename & ". " & _
  165.                     ONE_INSTANCE_ONLY
  166.        End If
  167.        WScript.Quit(0)
  168.     End If
  169.        EnumerateKey DefKey, Path, f1
  170.        f1.Close
  171.    End Sub
  172.  
  173.    ' Recursively enumerate registry and write it to a file.
  174.    Sub EnumerateKey(DefKey, Path, OutFile)
  175.        dim Keys, Names, types, i, j, value
  176.        OutFile.WriteLine("[" & Path & "]")
  177.        RegObj.EnumValues DefKey, Path, Names, Types
  178.        if not IsNull(Names) and not IsNull(Types) Then
  179.            for i = lbound(types) to ubound(types)
  180.                select case types(i)
  181.                    case 1
  182.                        RegObj.GetStringValue defkey, path, names(i), value
  183.                        If not isnull(names(i)) or not isnull(value) then
  184.                            OutFile.WriteLine  names(i) & "=" & Quote(value)
  185.                        end if
  186.                    case 2
  187.                        RegObj.GetExpandedStringValue defkey, path, names(i), _
  188.                                              value
  189.                        if not isnull(names(i)) or not isnull(value) then
  190.                            OutFile.WriteLine Quote(names(i)) & "=expand:" & _
  191.                                              Quote(value)
  192.                        end if
  193.                    case 3
  194.                        RegObj.GetBinaryValue defkey, path, names(i), value
  195.                        for j = lbound(value) to ubound(value)
  196.                            value(j) = hex(cint(value(j)))
  197.                        next
  198.                        if not isnull(names(i)) or not isnull(value) then
  199.                            OutFile.WriteLine Quote(names(i)) &"=hex:"& _
  200.                                              join(value, ",")
  201.                        end if
  202.                    case 4
  203.                        RegObj.GetDWordValue defkey, path, names(i), value
  204.                        if not isnull(names(i)) or value then
  205.                            OutFile.WriteLine Quote(names(i)) & "=dword:" & _
  206.                                              hex(value)
  207.                        end if
  208.                end select
  209.            next
  210.        end if
  211.  
  212.        OutFile.WriteLine
  213.        RegObj.EnumKey HKLM, Path, Keys
  214.        Dim SubKey, NewPath
  215.        If not IsNull(Keys) Then
  216.            For Each SubKey In Keys
  217.                NewPath = Path & "\" & SubKey
  218.                 EnumerateKey DefKey, NewPath,OutFile
  219.             Next
  220.         End if
  221.     End Sub
  222.  
  223.     ' Run a command and save the output to a file
  224.    Sub RunCmd(cmd, outfile)
  225.        Dim f1, run, output
  226.  
  227.     On Error Resume Next
  228.  
  229.     Set f1 = fso.CreateTextFile(outfile, True, True)
  230.     If Err.number <> 0 Then
  231.        If Not quietMode Then
  232.           WriteLine ""
  233.           WriteLine "Could not create text file " & outfile & ". " & _
  234.                     ONE_INSTANCE_ONLY
  235.         End If
  236.         WScript.Quit(0)
  237.     End If
  238.        set run = Wsh.exec(cmd)
  239.        output = run.stdout.readall
  240.        f1.Write output
  241.        f1.Close
  242.    End Sub
  243.  
  244.    Sub CopyConfig()
  245.        On Error Resume Next
  246.        Dim myFolders, folder
  247.        Set myFolders = fso.GetFolder(AppData & "\VMware").SubFolders
  248.        For Each folder in myFolders
  249.            folder = Fso.GetFileName(folder)
  250.            If LCase(folder) <> "vmware ace" Then
  251.                CopyFolderExceptVMs AppData & "\VMware\" & folder, _
  252.                                    workdir & "\Global_Config" & "\" & folder
  253.            End if
  254.        Next
  255.  
  256.        Fso.CopyFolder UserData & "\VMware", workdir & "\Current_User"
  257.        ' The UI Log
  258.         Fso.CopyFile tmpdir & "\vmware-" & Username & "\vmware*.log", _
  259.                      workdir & "\Temp\"
  260.        ' The Installer Logs
  261.     Fso.CopyFile SysTemp & "\vpxainst.log", workdir & "\SYSTEMP\"
  262.     Fso.CopyFile SysTemp & "\vpxauninst.log", workdir & "\SYSTEMP\"
  263.     Fso.CopyFile SysTemp & "\vim-vpxa-msi.log", workdir & "\SYSTEMP\"
  264.     Fso.CopyFile SysTemp & "\vminst.log", workdir & "\SYSTEMP\"
  265.        Fso.CopyFile tmpdir & "\vm*.log", workdir & "\Temp\"
  266.         Fso.CopyFile  SysTemp & "\vmware*.log", workdir & "\SYSTEMP\"
  267.        If (Fso.FileExists(WindowsDir & "\setupapi.log")) Then
  268.           Fso.CopyFile WindowsDir & "\setupapi.log" , workdir & "\SYSTEMP\"
  269.         End If
  270.         If (Fso.FileExists(WindowsDir & "\inf\setupapi.dev.log")) Then
  271.            Fso.CopyFile WindowsDir & "\inf\setupapi.dev.log" , workdir & "\SYSTEMP\"
  272.        End If
  273.        If (Fso.FileExists(WindowsDir & "\inf\setupapi.offline.log")) Then
  274.           Fso.CopyFile WindowsDir & "\inf\setupapi.offline.log" , workdir & "\SYSTEMP\"
  275.         End If
  276.         If (Fso.FileExists(WindowsDir & "\inf\setupapi.app.log")) Then
  277.            Fso.CopyFile WindowsDir & "\inf\setupapi.app.log" , workdir & "\SYSTEMP\"
  278.        End If
  279.        On Error Goto 0
  280.    End Sub
  281.  
  282.    ' Copy dump files
  283.    Sub CopyDumpFiles()
  284.     On Error Resume Next
  285.     Dim appDir
  286.        appDir = tmpDir & "\vmware-" & Username
  287.        Fso.CopyFile appDir & "\*.dmp", workdir & "\Dumps\"
  288.         appDir = GetUserProfileDirectory("Default User") & "\Application Data"
  289.         Fso.CopyFile appDir & "\VMware\*.dmp", workdir & "\Dumps\"
  290.        appDir = GetUserProfileDirectory("Default User") & _
  291.           "\Local Settings\Application Data"
  292.        Fso.CopyFile appDir & "\VMware\*.dmp", workdir & "\Dumps\"
  293.         appDir = GetUserProfileDirectory("LocalService") & "\Application Data"
  294.         Fso.CopyFile appDir & "\VMware\*.dmp", workdir & "\Dumps\"
  295.        appDir = GetUserProfileDirectory("NetworkService") & "\Application Data"
  296.        Fso.CopyFile appDir & "\VMware\*.dmp", workdir & "\Dumps\"
  297.         appDir = GetUserProfileDirectory("NetworkService") & _
  298.            "\Local Settings\Application Data"
  299.         Fso.CopyFile appDir & "\VMware\*.dmp", workdir & "\Dumps\"
  300.    End Sub
  301.  
  302.    Sub CopyEventLogs()
  303.     CopyLog "Application", workdir & "\Misc\"
  304.     CopyLog "System", workdir & "\Misc\"
  305.     CopyLog "Security", workdir & "\Misc\"
  306.     End Sub
  307.  
  308.     ' Delete files with sensitive data
  309.    ' Currently we delete the SSL folder to avoid collecting
  310.     ' the private or public keys.
  311.    Sub PurgeFiles()
  312.     On Error Resume Next
  313.     Fso.DeleteFolder workdir & "\Global_Config\VMware Server\SSL"
  314.    End Sub
  315.  
  316.    ' Copy the specified system event log to the specified directory
  317.     Sub CopyLog(logname, directory)
  318.         ' non-admin users would lack permissions
  319.        On Error Resume Next
  320.     Dim query1, query2, logfileset, logfileobj
  321.  
  322.     query1 = "winmgmts:{impersonationLevel=impersonate," &_
  323.           "(Backup,Security)}!\\.\root\cimv2"
  324.        query2 = "select * from Win32_NTEventLogFile where " &_
  325.           "LogfileName='" & logname & "'"
  326.  
  327.     Set logfileset = GetObject(query1).ExecQuery(query2)
  328.  
  329.     For Each logfileobj in logfileset
  330.        logfileobj.BackupEventLog(directory & logname & "-log.evt")
  331.        Next
  332.        On Error Goto 0
  333.    End Sub
  334.  
  335.    Sub CopyMinidump()
  336.        On Error Resume Next
  337.        Dim dumpFolder, myDumpFiles, dumpFile
  338.        Dim lastModTime1, lastModFile1 ' File modified last but one
  339.         Dim lastModTime2, lastModFile2 ' File modified last
  340.  
  341.        lastModTime1 = 0
  342.        lastModTime2 = 0
  343.        lastModFile1 = lastModFile2 = ""
  344.  
  345.        Set dumpFolder = fso.GetFolder(Minidump)
  346.        Set myDumpFiles = dumpFolder.Files
  347.        For Each dumpFile in myDumpFiles
  348.            If UCase(fso.GetExtensionName(dumpFile)) = "DMP" Then
  349.                If dumpFile.LastModified > lastModified2 Then
  350.                  lastModFile1 = lastModFile2
  351.                  lastModTime1 = lastModTime2
  352.                  lastModFile2 = dumpFile.Name
  353.                  lastModTime2 = dumpFile.LastModified
  354.                ElseIf dumpFile.LastModified > lastModified1 Then
  355.                  lastModFile1 = dumpFile.Name
  356.                  lastModTime1 = dumpFile.LastModified
  357.                End If
  358.            End If
  359.        Next
  360.  
  361.        If  lastModFile1 <> "" Then
  362.            Fso.CopyFile  MiniDump & "\" & lastModFile1 , workdir & "\Minidump\"
  363.            If Not quietMode Then Write "."
  364.        End If
  365.        If lastModFile2 <> "" Then
  366.            Fso.CopyFile  MiniDump & "\" & lastModFile2 , workdir & "\Minidump\"
  367.            If Not quietMode Then Write "."
  368.        End If
  369.        On Error Goto 0
  370.    End Sub
  371.  
  372.    Sub CopyVmwareSystemFiles()
  373.        On Error Resume Next
  374.        Dim dumpFolder, myDumpFiles, dumpFile
  375.  
  376.        Set dumpFolder = fso.GetFolder(SysTemp_VmwareSys)
  377.        Set myDumpFiles = dumpFolder.Files
  378.        For Each dumpFile in myDumpFiles
  379.            Fso.CopyFile  SysTemp_VmwareSys & "\" & dumpFile.Name , _
  380.                workdir & "\SYSTEMP\vmware-system\"
  381.            If Not quietMode Then Write "."
  382.        Next
  383.        On Error Goto 0
  384.    End Sub
  385.  
  386.  
  387.    ' Return a list of all matched objects
  388.     Function ReFindAll(regexp, data)
  389.         Dim re
  390.         Set re = New RegExp
  391.         re.Pattern = regexp
  392.         re.IgnoreCase = True
  393.         re.Global = True
  394.         Set ReFindAll = re.Execute(data)
  395.     End Function
  396.  
  397.     Function ReMatch(regexp, data)
  398.         Dim re
  399.         Set re = New RegExp
  400.         re.Pattern = regexp
  401.         re.IgnoreCase = True
  402.         Set ReMatch = re.Match(data)
  403.     End Function
  404.  
  405.     ' Unquote the string.
  406.    Function GetFileName(ByVal data)
  407.        GetFileName = split(data, Chr(34), 3)(1)
  408.    End Function
  409.  
  410.   ' Return the path to the user profile directory of the specified user
  411.     Function GetUserProfileDirectory(userName)
  412.     Dim strKeyPath, strValueName, strValue
  413.     strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
  414.     strValueName = "ProfilesDirectory"
  415.     RegObj.GetExpandedStringValue HKLM, strKeyPath, strValueName, strValue
  416.     If not IsNull(strValue) Then
  417.        strValue = strValue & "\" & userName
  418.     End if
  419.     GetUserProfileDirectory = strValue
  420.     End Function
  421.  
  422.     ' Search for all VMs registered with hostd and copy the contents by calling CopyVM
  423.     Sub CopyHostdVMs()
  424.     Dim xmlDoc, vms, i, inventoryFile, vmxFile
  425.     Set xmlDoc = CreateObject("Microsoft.XMLDOM")
  426.     inventoryFile = AppData & "\VMware\VMware Server\hostd\vmInventory.xml"
  427.     xmlDoc.Load(inventoryFile)
  428.     xmlDoc.async = false
  429.     Set vms = xmlDoc.GetElementsByTagName("vmxCfgPath")
  430.     For i = 0 to vms.length - 1
  431.         vmxFile = vms(i).text
  432.         If Fso.FileExists(vmxFile) Then
  433.               CopyVM i, vmxFile
  434.         End if
  435.     Next
  436.    End Sub
  437.  
  438.    ' Search only one level of folders for vmx files.
  439.    Sub FindAllFoldersWithVMX(directory)
  440.        On Error Resume Next
  441.        Dim subfolder, file
  442.        For Each subfolder in fso.GetFolder(directory).SubFolders
  443.            file = subfolder & "\v.vmx"
  444.            If not VMList.Exists(file) Then
  445.                VMList.add file, ""
  446.            End If
  447.        Next
  448.    End Sub
  449.  
  450.    ' Search for all VMs. The real work is done in CopyVM
  451.    Sub CopyVMs()
  452.        Dim file, data, m, i, packagepath, folder, subfolders, subfolder
  453.        If Wscript.Arguments.Count > 0 Then
  454.            If(collectUILogs) Then
  455.                Exit Sub
  456.            End If
  457.        End If
  458.  
  459.       If(collectLogsForVMs) Then
  460.            For i = 0 To Wscript.Arguments.Count - 1
  461.                If Wscript.Arguments(i) = "-v" _
  462.                    And i + 1 < Wscript.Arguments.Count _
  463.                Then
  464.                    Dim files
  465.                    files = Split(Wscript.Arguments(i + 1), "|")
  466.                    For Each file in files
  467.                        file = Trim(file)
  468.                        If InStrRev(file, ".vmx", -1, vbTextCompare) _
  469.                            <> (Len(file) - 3) _
  470.                        Then
  471.                            'Looks like we got the vmx file path
  472.                            file = file & "\v.vmx"
  473.                        End If
  474.                        If Not VMList.Exists(file) Then
  475.                            VMList.add file, ""
  476.                        End If
  477.                    Next
  478.                End If
  479.            Next
  480.        Else
  481.            file = UserData & "\VMware\preferences.ini"
  482.            If Fso.FileExists(file) Then
  483.                data = Fso.OpenTextFile(file).ReadAll
  484.                For Each m in ReFindAll(".*openedObj\d+\.file\s*=.*\n", data)
  485.                    file = GetFileName(m.value)
  486.                    IF not VMList.Exists(file) Then
  487.                        VMList.add file, ""
  488.                    End IF
  489.                Next
  490.            End If
  491.            file = UserData & "\VMware\inventory.vmls"
  492.            If Fso.FileExists(file) Then
  493.                data = Fso.OpenTextFile(file).ReadAll
  494.                For Each m in ReFindAll("vmlist\d+\.config\s*=.*\n", data)
  495.                    file = GetFileName(m.value)
  496.                    If not VMList.Exists(file) Then
  497.                        VMList.add file, ""
  498.                    End If
  499.                Next
  500.            End If
  501.            file = UserData & "\VMware\Virtual Machines.vmls"
  502.            If Fso.FileExists(file) Then
  503.                data = Fso.OpenTextFile(file).ReadAll
  504.                For Each m in ReFindAll("vmlist\d+\.config\s*=.*\n", data)
  505.                    file = GetFileName(m.value)
  506.                    If not VMList.Exists(file) Then
  507.                        VMList.add file, ""
  508.                    End If
  509.                Next
  510.            End If
  511.            ' Collect ACE 1.0 vms
  512.            RegObj.GetStringValue HKLM, _
  513.                "SOFTWARE\VMware, Inc.\VMware ACE", _
  514.                "PackagePath", packagepath
  515.            If not isNull(packagepath) _
  516.                and Fso.FolderExists(packagepath & "Virtual Machines\") _
  517.             Then
  518.                 Set folder = Fso.GetFolder(packagepath & "Virtual Machines\")
  519.                FindAllFoldersWithVMX(folder)
  520.            End If
  521.            ' Collect ACE 2.0 deployed VMs
  522.            FindAllFoldersWithVMX(AppData & "\VMware\VMware ACE")
  523.        End If
  524.  
  525.        CollectStateLogs = False
  526.        For i = 0 To Wscript.Arguments.Count - 1
  527.            If Wscript.Arguments(i) = "-s" Then
  528.                CollectStateLogs = True
  529.                Exit For
  530.            End If
  531.        Next
  532.  
  533.        VMList = VMList.Keys
  534.        For i = Lbound(VMList) to Ubound(VMList)
  535.            If Fso.FileExists(VMList(i)) _
  536.               OR ContainsVmxFile(Fso.GetParentFolderName(VMList(i))) _
  537.            Then
  538.                CopyVM i, VMList(i)
  539.            End If
  540.        Next
  541.  
  542.     CopyHostdVMs
  543.    End Sub
  544.  
  545.    Sub CopyFolderExceptVMs(source, dest)
  546.        Dim file, folder, myFolders, tmpf
  547.        ' create dest if it does not exist
  548.        If not Fso.FolderExists(dest) Then
  549.           Fso.CreateFolder dest
  550.        End If
  551.        ' Copy each file in the directory
  552.        For Each file in Fso.GetFolder(source).Files
  553.            tmpf = LCase(file)
  554.            ' Let's keep this as a whitelist for now.
  555.            ' .gz added to collect the zipped hostd files
  556.            If right(tmpf,4) = ".log" OR _
  557.               right(tmpf,3) = ".gz" OR _
  558.               right(tmpf,4) = ".ini" OR _
  559.               right(tmpf,4) = ".xml" Then
  560.               Fso.CopyFile file, dest & "\"
  561.             End If
  562.         Next
  563.  
  564.         If Not quietMode Then Write "."
  565.         ' Copy each directory in this directory
  566.        Set myFolders = fso.GetFolder(source).SubFolders
  567.        For Each folder in myFolders
  568.            folder = Fso.GetFileName(folder)
  569.            If LCase(folder) <> "virtual machines" Then
  570.                CopyFolderExceptVMs source & "\" & folder, _
  571.                                    dest & "\" & folder
  572.            End If
  573.        Next
  574.    End Sub
  575.  
  576.    ' Save files less than 30K or the log file.
  577.     ' Monitor logs will be in a subdirectory eventually.
  578.    Sub CopyVM(index, vmx)
  579.        Dim src, dst, f, i, baseDst, absPath, pos
  580.        Dim myFld, folder
  581.  
  582.        On Error Resume Next
  583.  
  584.        src = Fso.GetParentFolderName(vmx)
  585.        ' Set the destination dir to be the VM folder name.
  586.         ' If there are more than one VMs with the same folder name
  587.        ' we add a "-1", "-2" etc as the suffix.
  588.         dst = workdir & "\VM\" & Fso.GetFolder(src).Name
  589.        baseDst = dst
  590.        i = 1
  591.        While Fso.FolderExists(dst)
  592.            dst = baseDst & "-" & i
  593.            i = i + 1
  594.        Wend
  595.        dst = dst & "\"
  596.         Fso.CreateFolder(dst)
  597.         For Each f in fso.GetFolder(src).Files
  598.             f = LCase(f)
  599.             absPath = f
  600.             ' get file name from absolute path
  601.            pos = InStrRev(f, "\")
  602.            f = right(f, Len(f) - pos)
  603.            If Fso.GetFile(absPath).Size < 30000 OR right(f,5)=".vmpl" _
  604.         OR left(f,5)="stats" OR left(f,4)="gmon" OR left(f,10)="callstacks"   _
  605.         OR left(f,7)="samples" OR left(f,6)="status" Then
  606.                Fso.CopyFile absPath, dst
  607.            Else
  608.                If right(f,4)=".log" Then
  609.                    If CollectStateLogs=True Then
  610.                        Fso.CopyFile absPath, dst
  611.                    Else
  612.                        If InStr(f, "state")=0 Then
  613.                             Fso.CopyFile absPath, dst
  614.                        End If
  615.                    End If
  616.                ElseIf right(f,4)=".dmp" Then
  617.                    Fso.CopyFile f, dst
  618.                End If
  619.            End If
  620.        Next
  621.  
  622.        Set myFld = Fso.GetFolder(src).SubFolders
  623.        For Each folder in myFld
  624.             Dim tmpf
  625.             folder = Fso.GetFileName(folder)
  626.             tmpf = LCase(folder)
  627.             If right(tmpf,5) = "stats" Then
  628.                Fso.CopyFolder src & "\" & folder, dst & "\stats"
  629.             End If
  630.        Next
  631.  
  632.        RunCmd "cacls """ & src & """\*.*", dst & "cacls.txt"
  633.  
  634.        Set f = fso.CreateTextFile(dst & "vmxpath.txt", True, True)
  635.     If Err.number <> 0 Then
  636.        If Not quietMode Then
  637.           WriteLine ""
  638.           WriteLine "Could not create text file vmxpath.txt. " & _
  639.                     ONE_INSTANCE_ONLY
  640.        End If
  641.        WScript.Quit(0)
  642.     End If
  643.        f.WriteLine vmx
  644.        f.Close
  645.  
  646.        If Not quietMode Then Write "."
  647.    End Sub
  648.  
  649.    ' returns TRUE if there's a VMX file in the given directory
  650.    Function ContainsVmxFile(path)
  651.        Dim f
  652.        ContainsVmxFile = FALSE
  653.        If not Fso.FolderExists(path) Then
  654.            Exit Function
  655.        End If
  656.        For Each f in fso.GetFolder(path).Files
  657.            f = LCase(f)
  658.            If right(f,4)=".vmx" Then
  659.                ContainsVmxFile = TRUE
  660.                Exit Function
  661.            End If
  662.        Next
  663.    End Function
  664.  
  665.    ' Save the MSinfo report, this takes a while and hence not saving text.
  666.     Sub MSInfo
  667.         Dim msinfo
  668.         msinfo = Wsh.RegRead("HKLM\SOFTWARE\Microsoft\Shared Tools\MSInfo\Path")
  669.         Wsh.Run Quote(msinfo) & " /nfo " & workdir & "\Misc\MSinfo.nfo", 0, True
  670.         If Not quietMode Then Write "."
  671.     End Sub
  672.  
  673.     Sub Service
  674.         Dim fp, wmi, s, Services, i
  675.  
  676.     On Error Resume Next
  677.  
  678.         Set fp = Fso.CreateTextFile(workdir & "\Misc\Service.txt", _
  679.                                     True, True)
  680.     If Err.number <> 0 Then
  681.        If Not quietMode Then
  682.           WriteLine ""
  683.           WriteLine "Could not create text file Service.txt. " & _
  684.                     ONE_INSTANCE_ONLY
  685.        End If
  686.        WScript.Quit(0)
  687.     End If
  688.         Set wmi = GetObject("winmgmts:" _
  689.             & "{impersonationLevel=impersonate}!\\.\root\cimv2")
  690.         Set Services = wmi.ExecQuery _
  691.                 ("SELECT * FROM Win32_Service")
  692.         For Each s in Services
  693.             fp.WriteLine "System Name: " & s.SystemName
  694.             fp.WriteLine "Service Name: " & s.Name
  695.             fp.WriteLine "Service Type: " & s.ServiceType
  696.             fp.WriteLine "Service State: " & s.State
  697.             fp.WriteLine "ExitCode: " & s.ExitCode
  698.             fp.WriteLine "Process ID: " & s.ProcessID
  699.             fp.WriteLine "Accept Pause: " & s.AcceptPause
  700.             fp.WriteLine "Accept Stop: " & s.AcceptStop
  701.             fp.WriteLine "Caption: " & s.Caption
  702.             fp.WriteLine "Description: " & s.Description
  703.             fp.WriteLine "Desktop Interact: " & s.DesktopInteract
  704.             fp.WriteLine "Display Name: " & s.DisplayName
  705.             fp.WriteLine "Error Control: " & s.ErrorControl
  706.             fp.WriteLine "Path Name: " & s.PathName
  707.             fp.WriteLine "Started: " & s.Started
  708.             fp.WriteLine "StartMode: " & s.StartMode
  709.             fp.WriteLine "StartName: " & s.StartName
  710.             fp.Writeline
  711.  
  712.             i=i+1
  713.             if (i mod 4) = 0 And Not quietMode then Write "."
  714.         Next
  715.         fp.Close
  716.     End Sub
  717.  
  718.     Sub VMwareUser
  719.         Dim accounts, user, localhost, vmuser
  720.         localhost = CreateObject("Wscript.Network").ComputerName
  721.         Set vmuser = GetObject("WinNT://" & localhost & "/__vmware_user__")
  722.     End Sub
  723.  
  724.     Sub CopyVRMServerLogs
  725.         Dim ACEMgmtServ
  726.         ACEMgmtServ = ProgFiles & "\VMware\VMware ACE Management Server"
  727.         If Fso.FolderExists(ACEMgmtServ) Then
  728.             On Error Resume Next
  729.             Fso.CreateFolder(workdir & "\AceServer\apacheConfig")
  730.             CopyFolderExceptVMs ACEMgmtServ & "\Apache2\conf" , _
  731.                           workdir & "\AceServer\apacheConfig"
  732.             Fso.CreateFolder(workdir & "\AceServer\apacheLog")
  733.             CopyFolderExceptVMs ACEMgmtServ & "\Apache2\logs" , _
  734.                           workdir & "\AceServer\apacheLog"
  735.             Fso.CreateFolder(workdir & "\AceServer\aceConfig")
  736.             Fso.CopyFile ACEMgmtServ & "\conf\acesc.conf",_
  737.                           workdir & "\AceServer\aceConfig\"
  738.            Fso.CopyFile ACEMgmtServ & "\Apache2\cgi-bin\acesc-config.conf",_
  739.                          workdir & "\AceServer\aceConfig\"
  740.             Fso.CreateFolder(workdir & "\AceServer\aceLog")
  741.             Fso.CopyFile ACEMgmtServ & "\log\acesc-config.log" , _
  742.                           workdir & "\AceServer\aceLog\"
  743.        End If
  744.    End Sub
  745.  
  746.    Sub BootIni
  747.        Dim i, bootdrive, bootini
  748.        For i=0 to 23
  749.            bootdrive = Chr(Asc("C")+i)
  750.            bootini = bootdrive & ":\boot.ini"
  751.            If Fso.FileExists(bootini) Then
  752.                On Error Resume Next
  753.         Dim bootinidest, f
  754.         bootinidest = workdir & "\Misc\" & bootdrive & "_boot.ini"
  755.                 Fso.CopyFile  bootini, bootinidest
  756.  
  757.         ' Unset the hidden and system bits if set.
  758.         Set f = Fso.GetFile(bootinidest)
  759.         If f.attributes and 4 Then
  760.             f.attributes = f.attributes - 4
  761.         End If
  762.         If f.attributes and 2 Then
  763.             f.attributes = f.attributes - 2
  764.         End If
  765.                ' GetFile would fail if the boot.ini was not copied
  766.                 On Error Goto 0
  767.                 If bootdrive = "C" Then
  768.                     Exit For
  769.                 End If
  770.             End If
  771.         Next
  772.     End Sub
  773.  
  774.     Sub Generate()
  775.     If Not quietMode Then
  776.         WriteLine "Collecting information needed for support. " & _
  777.         "This may take several minutes."
  778.     End If
  779.  
  780.     Set Fso = CreateObject("Scripting.FileSystemObject")
  781.  
  782.     If Not quietMode Then Write "  Registry "
  783.     DumpKey HKLM, "SOFTWARE\VMware, Inc.", workdir & "\Misc\vmware_reg.txt"
  784.     If Not quietMode Then Write ".."
  785.     DumpKey HKLM, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards", _
  786.     workdir & "\Misc\networkcards_reg.txt"
  787.     If Not quietMode Then WriteLine "."
  788.  
  789.     If Not quietMode Then Write "  DxDiag Data ."
  790.     Wsh.Run("dxdiag /t " & workdir & "\DxDiag\DxDiag.txt")
  791.  
  792.     'Version Check for Windows 8 current version added
  793.     ' to be modified when /64bit option is not supported later on too
  794.     If archVersion = 64 And (majorVersion <= 5 or (majorVersion = 6 _
  795.                             And minorVersion < 2)) Then
  796.        Wsh.Run("dxdiag /64bit /t" & workdir & "\DxDiag\DxDiag-64Bit.txt")
  797.     End if
  798.     If Not quietMode Then WriteLine "."
  799.  
  800.     If Not quietMode Then Write "  Network Configuration ."
  801.         Fso.CopyFile AppData & "\VMware\vmnet*.*" , workdir & "\Misc"
  802.     RunCmd "ipconfig /all", workdir & "\Misc\ipconfig.txt"
  803.     RunCmd "route print", workdir & "\Misc\route.txt"
  804.     If Not quietMode Then Write ".."
  805.     RunCmd "netstat -aens", workdir & "\Misc\netstat.txt"
  806.     If Not quietMode Then WriteLine ".."
  807.     RunCmd "netsh winsock show catalog", workdir & "\Misc\winsock_catalog.txt"
  808.     If Not quietMode Then WriteLine ".."
  809.  
  810.     If Not quietMode Then Write "  Startup Settings ."
  811.     BootIni
  812.     If Not quietMode Then WriteLine "."
  813.  
  814.     If Not quietMode Then Write "  VMware Configuration ."
  815.     CopyConfig
  816.     CopyDumpFiles
  817.     If Not quietMode Then WriteLine "."
  818.  
  819.     If Not quietMode Then Write "  Virtual Machines "
  820.     CopyVMs
  821.     If Not quietMode Then WriteLine ""
  822.  
  823.     If Not quietMode Then Write "  Services "
  824.     Service
  825.     If Not quietMode Then WriteLine ""
  826.  
  827.     If Not quietMode Then Write "  Dumps ."
  828.     CopyMinidump
  829.     CopyVmwareSystemFiles
  830.     If Not quietMode Then WriteLine ""
  831.  
  832.     If Not quietMode Then Write "  System Information ."
  833.     MSInfo
  834.     If Not quietMode Then WriteLine "."
  835.  
  836.     If Not quietMode Then Write "  Logs ."
  837.     CopyEventLogs
  838.     CopyVRMServerLogs
  839.     If Not quietMode Then WriteLine ""
  840.  
  841.     PurgeFiles
  842.  
  843.     If Not quietMode Then WriteLine ""
  844.  
  845.     Dim zipfile
  846.     zipfile = workdir & ".zip"
  847.     If Not quietMode Then WriteLine "Creating Zip File"
  848.     RunCmd zipExe & " -r " & zipfile & " " & workdir, tmpDir & "\out.txt"
  849.  
  850.     If Not quietMode Then WriteLine ""
  851.  
  852.     If Not Fso.FileExists(zipfile) Then
  853.         If Not quietMode Then
  854.            WriteLine "There was an error creating the zip file."
  855.            WriteLine "If a file changed while reading, " &_
  856.                    "please run this script again."
  857.            WriteLine ""
  858.         End If
  859.     Else
  860.         If Not quietMode Then
  861.            WriteLine "Done!"
  862.            WriteLine ""
  863.         End If
  864.         If Fso.GetFile(zipfile).size > 10000000 Then
  865.             If Not quietMode Then
  866.                WriteLine "NOTE: " & zipfile & " is greater than 10 MB. "
  867.                WriteLine "Please do not attach this file when submitting an " &_
  868.                        "incident report. Please contact VMware support for " &_
  869.                        "an ftp site. To file a support incident, go to " &_
  870.                        "http://www.vmware.com/support/sr/sr_login.jsp"
  871.             End If
  872.         Else
  873.             WriteLine "File: " & zipfile
  874.             If Not quietMode Then
  875.                WriteLine "Please attach this file when submitting an incident " &_
  876.                        "report. To file a support incident, go to " &_
  877.                        "http://www.vmware.com/support/sr/sr_login.jsp"
  878.             End If
  879.         End If
  880.         If Not quietMode Then WriteLine ""
  881.     End If
  882.  
  883.     on error resume next
  884.     If Fso.FolderExists(workdir) Then
  885.         Fso.DeleteFolder workdir, true
  886.     End If
  887.  
  888.     'Open Windows Explorer in the right folder
  889.    If Not quietMode Then Wsh.exec("explorer /n,/select," & zipfile)
  890.    on error goto 0
  891.    If Not quietMode Then WriteLine ""
  892.    If Not quietMode Then WriteLine ""
  893.  
  894.    End Sub
  895.  
  896.  
  897. End class
  898.  
  899. sub Write (s)
  900.    on error resume next
  901.     Wscript.StdOut.Write s
  902.     on error goto 0
  903. end sub
  904.  
  905. sub WriteLine (s)
  906.    on error resume next
  907.     Wscript.StdOut.WriteLine s
  908.     on error goto 0
  909. end sub
  910.  
  911. ' If running with Wscript, relaunch with Cscript.
  912. ' This way people who double click directly on the script get the console and
  913. ' not a barrage of alert boxes.
  914. sub EnsureCscript
  915.     If Not WScript.FullName = WScript.Path & "\cscript.exe" Then
  916.         Dim Shell
  917.         Set Shell = CreateObject("WScript.Shell")
  918.         Shell.Run WScript.Path & "\cscript.exe " & _
  919.             Chr(34) & WScript.ScriptFullName & Chr(34) & _
  920.             " //Nologo " & FormatArguments(), _
  921.             1, False
  922.         WScript.Quit 0
  923.     End If
  924. end sub
  925.  
  926. Function IsArgsAvail
  927.     Dim args, idx, arg, check
  928.     Set args = WScript.Arguments
  929.     idx = 0
  930.     check = True
  931.  
  932.     While idx < args.Count
  933.         arg = args(idx)
  934.         Select Case arg
  935.             Case "-q"
  936.                 quietMode = True
  937.             Case "-w"
  938.                 idx = idx + 1
  939.                 If idx = args.Count Then
  940.                     check = False
  941.                 Else
  942.                     outputFolder = args(idx)
  943.                 End If
  944.             Case "-u"
  945.                 collectUILogs = True
  946.             Case "-s"
  947.                 'go to next argument
  948.            Case "-v"
  949.                idx = idx + 1
  950.                'check if next argument is provided
  951.                 If idx = args.Count Then
  952.                     check = False
  953.                 Else
  954.                     collectLogsForVMs = True
  955.                 End If
  956.             Case "-h"
  957.                 check = False
  958.             Case Else
  959.                 check = False
  960.         End Select
  961.  
  962.         If Check = False Then
  963.             IsArgsAvail = False
  964.             Exit Function
  965.         End If
  966.  
  967.         idx = idx + 1
  968.     Wend
  969.     IsArgsAvail = check
  970. End Function
  971.  
  972. Sub Usage
  973.     Wscript.Echo "Usage: vm-support.vbs [-q] [-w <output path>] [-s|-u|-v<path>] [-h]"
  974.     Wscript.Echo "  -q               Quiet mode. Do not print any unnecessary information"
  975.     Wscript.Echo "  -w               Pass a directory path where the result will be saved"
  976.     Wscript.Echo "  -u               Collect system information and UI logs"
  977.     WriteLine ""
  978.     Wscript.Echo "  -v VMX filename(s) or dir path(s) separated by |"
  979.     Wscript.Echo "                   Collect logs only for the specified VMs"
  980.     Wscript.Echo "                   Example: vm-support.vbs -v ""path1\file1 | path2\file2"""
  981.     WriteLine ""
  982.     Wscript.Echo "  -s               Include state logs to vm-support package"
  983.     WriteLine ""
  984.     Wscript.Echo "  -h               Display this help menu"
  985.     WriteLine ""
  986.     Wscript.Echo " If no options are given, collect logs for all VMs"
  987.     WriteLine ""
  988.     WriteLine "Press 'Enter' to close this window"
  989.     Dim read
  990.     read = Wscript.StdIn.Read(1)
  991.     Wscript.Quit 0
  992. End Sub
  993.  
  994. ' Convert wscript version number if system uses comma as the decimal point.
  995. Dim wversion
  996. wversion = Wscript.Version
  997. if not IsNumeric(wversion) and IsNumeric(Replace(wversion,".",",")) Then
  998.    wversion = CDbl(Replace(wversion,".",","))
  999. End If
  1000.  
  1001. If wversion < 5.6 Then
  1002.    Wscript.Echo "This vm-support script expects Windows Script Version " & _
  1003.                 "5.6 or above"
  1004.    Wscript.Echo "Windows Script update can be obtained from:"
  1005.    Wscript.Echo "http://www.microsoft.com/downloads/details.aspx" & _
  1006.                 "?FamilyId=C717D943-7E4B-4622-86EB-95A22B832CAA&displaylang=en"
  1007.    Wscript.quit 0
  1008. End If
  1009.  
  1010. EnsureCscript
  1011.  
  1012. ' Parse command line arguments
  1013. quietMode = False
  1014. outputFolder = ""
  1015. collectUILogs = False
  1016. collectLogsForVMs = False
  1017.  
  1018. If Not IsArgsAvail Then
  1019.     Call Usage
  1020. Else
  1021.  
  1022.     If Not quietMode Then
  1023.         WriteLine "VMware Support Script"
  1024.         WriteLine "Copyright (C) 1998-2011 VMware, Inc."
  1025.         WriteLine "Warning: This script will collect most files in the Virtual Machines folder."
  1026.         WriteLine "         Move any sensitive information to another folder."
  1027.         WriteLine ""
  1028.     End If
  1029.  
  1030.     Dim info
  1031.     Set info = new VMsupport
  1032.     info.Generate
  1033. End If
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement