8

我正在尝试编写一个 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
4

3 回答 3

16

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

于 2013-06-19T14:56:21.677 回答
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
于 2014-01-01T19:56:59.703 回答
2

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

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

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

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

于 2015-07-20T23:58:21.017 回答