我需要将VBS WScript.Echo输出写入text或cvs

时间:2013-06-19 14:48:30

标签: csv text vbscript echo wsh

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

以下是我找到的脚本:

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

3 个答案:

答案 0 :(得分:15)

解决此问题的一种方法是通过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

答案 1 :(得分:2)

Skarykid - 我知道你差不多6个月前收到并回答了这个问题,但我一直在寻找类似的剧本&amp;没有取得太大的成功,正是我想要的。我修改了你提供的脚本,添加了Ansgar的建议&amp;我自己的一些代码来提出以下加强版本。

此脚本将生成安装在本地计算机上的所有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

答案 2 :(得分:0)

除了安斯加最受欢迎的答案之外,还要改变

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

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

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