I need to write VBS WScript.Echo output to text or cvs

前端 未结 3 1849
广开言路
广开言路 2021-01-12 04:19

I am attempting to write a VBScript that will list all of the installed application on a system in a text file or csv. I was able to find an existing code to list all of the

相关标签:
3条回答
  • 2021-01-12 05:06

    One way to go about this would be to run the script via cscript.exe and redirect the output to a file:

    cscript.exe //NoLogo "C:\path\to\your.vbs" >"C:\output.txt"
    

    If you want to modify the script to write its output to a file regardless of how it's run, you need to add code for opening/closing the output file:

    Dim fso
    Set fso = WScript.CreateObject("Scripting.Filesystemobject")
    Set f = fso.OpenTextFile("C:\output.txt", 2)
    
    ...
    
    f.Close
    'End of Script
    

    and replace each occurrence of WScript.Echo with f.WriteLine.

    0 讨论(0)
  • 2021-01-12 05:18

    In addition to Ansgar's top-rated answer, changing

    Set f = fso.OpenTextFile("C:\output.txt", 2)
    

    to

    Set f = fso.CreateTextFile("C:\output.txt", 2)
    

    allows VBS script to create the output files rather than requiring an existing blank file to already be in the destination.

    0 讨论(0)
  • 2021-01-12 05:19

    Skarykid - I know you received and answer to this almost 6 months ago, but I've been looking for a similar script & haven't had much success getting exactly what I wanted. I've modified the script you provided, added Ansgar's suggestions & a little code of my own to come up with the following souped-up version.

    This script will generate a tabulated text file of all 32bit and 64bit applications installed on the local machine & will then open the resulting file in notepad.

    Hope this helps you or anyone else coming across this thread.

    'listapps.vbs
    'Generates a text file listing all 32bit & 64bit apps installed on the local machine
    '===================================================================================
    
    'Declare constants, variables and arrays
    '---------------------------------------
    
    'Registry keys and values
    Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE 
    
    Dim arrKeys(1)
    arrKeys(0) = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
    arrKeys(1) = "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\"
    
    strComputer = "." 
    strEntry1a = "DisplayName" 
    strEntry1b = "QuietDisplayName" 
    strEntry2 = "Publisher"
    strEntry3 = "InstallDate"
    strEntry4 = "EstimatedSize"
    strEntry5 = "DisplayVersion"
    
    'Create the output file
    Dim objShell, objShellEnv, strComputerName, objFso
    Set objShell = WScript.CreateObject("WScript.Shell")
    Set objShellEnv = objShell.Environment("Process")
    strComputerName = objShellEnv("ComputerName")
    Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
    Set outputFile = objFso.CreateTextFile(strComputerName & ".txt", True)
    '===================================================================================
    
    Set objReg = GetObject("winmgmts://" & strComputer & "/root/default:StdRegProv") 
    
    'Print header (comment out the line below if you do not want headers in your output file)
    'outputFile.WriteLine"Name" & vbTab & "Publisher" & vbTab & "Installed On" & vbTab & "Size" & vbTab & "Version" & VbCrLf 
    
    For i = 0 to 1
    
      'Check to ensure registry key exists
      intCheckKey = objReg.EnumKey(HKLM, arrKeys(i), arrSubkeys)
    
      If intCheckKey = 0 Then
        For Each strSubkey In arrSubkeys 
          intReturn = objReg.GetStringValue(HKLM, arrKeys(i) & strSubkey, strEntry1a, strValue1) 
    
          If intReturn <> 0 Then 
            objReg.GetStringValue HKLM, arrKeys(i) & strSubkey, strEntry1b, strValue1 
          End If 
    
          objReg.GetStringValue HKLM, arrKeys(i) & strSubkey, strEntry2, strValue2
          objReg.GetStringValue HKLM, arrKeys(i) & strSubkey, strEntry3, strValue3
          objReg.GetDWORDValue HKLM, arrKeys(i) & strSubkey, strEntry4, strValue4
          objReg.GetStringValue HKLM, arrKeys(i) & strSubkey, strEntry5, strValue5
    
        If strValue1 <> "" Then
          outputFile.WriteLine strValue1 & vbTab & strValue2 & vbTab & strValue3 & vbTab & strValue4 & vbTab & strValue5
        End If
    
        Next 
    
      End If
    
    Next 
    
    'Close the output file
    outputFile.Close
    
    'Launch output file for review
    objShell.run "notepad.exe " & strComputerName & ".txt"
    
    'Clean up and exit
    Set objShell = Nothing
    Set objFso = Nothing
    
    0 讨论(0)
提交回复
热议问题