【问题标题】:I need to write VBS WScript.Echo output to text or cvs我需要将 VBS WScript.Echo 输出写入文本或 cvs
【发布时间】:2013-06-16 04:08:40
【问题描述】:

我正在尝试编写一个 VBScript,它将以文本文件或 csv 的形式列出系统上所有已安装的应用程序。我能够找到一个现有代码来列出所有软件(包括名称、版本、日期和大小)。当我当前运行它时,我发现它会在主机回声弹出时出现。我需要向 vbs 添加什么以使其将每个回声输出到文件?我确信这很容易,但我似乎找不到解决方案。

下面是我找到的脚本:

Dim fso
Set fso = WScript.CreateObject("Scripting.Filesystemobject")
' List All Installed Software





Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
strComputer = "."
strKey = "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"
strEntry1a = "DisplayName"
strEntry1b = "QuietDisplayName"
strEntry2 = "InstallDate"
strEntry3 = "VersionMajor"
strEntry4 = "VersionMinor"
strEntry5 = "EstimatedSize"

Set objReg = GetObject("winmgmts://" & strComputer & _
 "/root/default:StdRegProv")
objReg.EnumKey HKLM, strKey, arrSubkeys
WScript.Echo "Installed Applications" & VbCrLf
For Each strSubkey In arrSubkeys
  intRet1 = objReg.GetStringValue(HKLM, strKey & strSubkey, _
   strEntry1a, strValue1)
  If intRet1 <> 0 Then
    objReg.GetStringValue HKLM, strKey & strSubkey, _
     strEntry1b, strValue1
  End If
  If strValue1 <> "" Then
    WScript.Echo VbCrLf & "Display Name: " & strValue1
  End If
  objReg.GetStringValue HKLM, strKey & strSubkey, _
   strEntry2, strValue2
  If strValue2 <> "" Then
    WScript.Echo "Install Date: " & strValue2
  End If
  objReg.GetDWORDValue HKLM, strKey & strSubkey, _
   strEntry3, intValue3
  objReg.GetDWORDValue HKLM, strKey & strSubkey, _
   strEntry4, intValue4
  If intValue3 <> "" Then
     WScript.Echo "Version: " & intValue3 & "." & intValue4
  End If
  objReg.GetDWORDValue HKLM, strKey & strSubkey, _
   strEntry5, intValue5
  If intValue5 <> "" Then
    WScript.Echo "Estimated Size: " & Round(intValue5/1024, 3) & " megabytes"
  End If
Next

【问题讨论】:

    标签: csv text vbscript echo wsh


    【解决方案1】:

    解决此问题的一种方法是通过cscript.exe 运行脚本并将输出重定向到文件:

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

    如果您想修改脚本以将其输出写入文件而不管它是如何运行的,您需要添加用于打开/关闭输出文件的代码:

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

    并将每次出现的WScript.Echo 替换为f.WriteLine

    【讨论】:

      【解决方案2】:

      Skarykid - 我知道您在大约 6 个月前收到并回答了这个问题,但我一直在寻找类似的脚本,但并没有成功地得到我想要的。我已经修改了您提供的脚本,添加了 Ansgar 的建议和我自己的一些代码,以提供以下增强版本。

      此脚本将生成一个包含本地计算机上安装的所有 32 位和 64 位应用程序的表格文本文件,然后将在记事本中打开生成的文件。

      希望这可以帮助您或其他任何遇到此主题的人。

      '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
      

      【讨论】:

        【解决方案3】:

        除了Ansgar评分最高的答案,变化

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

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

        允许 VBS 脚本创建输出文件,而不是要求现有的空白文件已经在目标中。

        【讨论】:

        • 我已经仔细阅读了这两个链接,但不明白为什么这是“危险的”
        • 它隐藏了 .OpenTextFile 的第三个“create”参数,并错误地暗示 .CreateTextFile 的第二个参数是 io 模式。
        • 但是第一个不是 gerendasi 的声明,第二个按预期工作,尽管是错误和巧合。这就像不是一样糟糕。但是,如果他得到“-1”,那么稍微解释一下就可以了。将第三个参数 True 添加到 .OpenTextFile 意味着“如果不存在则创建”。 CreateTextFile 中的第二个参数是“覆盖”,您的数字 2 被强制转换为 True,因此它可以正常工作,但不像您那样。这两种方法虽然如前所述使用,但作用不同。一会添加,二会覆盖output.txt文件
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多