我需要将VBS WScript.Echo输出写入文本或CSV文件。

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
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


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

2
除了Ansgar的最佳答案之外,更改

标签可能会影响您的页面布局和样式。如果您不确定如何更改,请咨询您的网站开发人员或技术支持团队。他们可以帮助您避免潜在的问题并确保您的网站正常运行。
Set f = fso.OpenTextFile("C:\output.txt", 2)

to

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

允许 VBS 脚本创建输出文件,而不是要求目标位置已经存在一个空白文件。


请阅读 https://msdn.microsoft.com/en-us/library/314cz14s%28v=vs.84%29.aspx 和 https://msdn.microsoft.com/en-us/library/5t9b5c0c%28v=vs.84%29.aspx,了解为什么会出现这种危险的错误信息。 - Ekkehard.Horner
我仔细阅读了这两个链接,但不明白为什么这是“危险”的。 - gerendasi
它隐藏了.OpenTextFile的第三个“create”参数,并错误地暗示.CreateTextFile的第二个参数是io-mode。 - Ekkehard.Horner
1
但第一个不是gerendasi的陈述,第二个按预期工作,尽管是错误和巧合。这就像没有一样糟糕。但如果他因此得到“-1”,那么稍微解释一下会更合适。在.OpenTextFile中添加第三个参数True意味着“如果不存在则创建”。CreateTextFile中的第二个参数是“覆盖”,您的数字2被转换为True,所以它可以工作,但不是您想象的那样。尽管如此,这两种方法在刚才描述的情况下将有所不同。第一个将添加,第二个将覆盖output.txt文件。 - papo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接