【问题标题】:How to get VBS to read from text file?如何让 VBS 从文本文件中读取?
【发布时间】:2013-12-13 20:33:46
【问题描述】:

我希望将单个计算机脚本变成大众使用的脚本。它的作用是允许我通过从文本文件中提取来选择要卸载的 Windows Essentials 的哪些部分。然而,它只是提示输入一个单一的计算机名称......我希望它从我创建的 Computer_Names.txt 的单独文本文件中提取,并在该列表中的所有计算机上运行卸载。我似乎无法正确理解。

Const ForReading = 1
strComputer = InputBox("Enter the Computer Name:- ") 

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("C:\Windows_Live_Components.txt", ForReading)
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")


StrOutput = "Uninstalled the following Software:- "
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
arrSoftwareList = Split(strNextLine , ",")
Set colSoftware = objWMIService.ExecQuery ("Select * from Win32_Product Where Name = " & Chr(34) & arrSoftwareList(0) & Chr(34) )
For Each objSoftware in colSoftware
    objSoftware.Uninstall()
    strOutput = stroutput & vbCrLf & objsoftware.Name
Next
Loop
WScript.Echo strOutput

Computer_Names.txt 文件的内容就是这样。每台计算机上单独一行文本。

LAPD2712
LAPD2812

我们所有的计算机名称都是 4 个大写字母,4 个数字。

【问题讨论】:

  • 请发布文本文件的内容。
  • 刚刚编辑要显示的主要帖子。

标签: vbscript wmi uninstallation wmi-query


【解决方案1】:

你可以这样做:

Dim objFSO, objComponentsTextFile, objComputersTextFile, objWMIService, strComputer, StrOutput
Dim computerArray(), softwareArray, x
x = 0
Redim computerArray(x)


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objComponentsTextFile = objFSO.OpenTextFile ("C:\Windows_Live_Components.txt", 1)
Set objComputersTextFile = objFSO.OpenTextFile ("C:\Computers.txt", 1)

Do Until objComputersTextFile.AtEndOfStream
    computerArray(x) = objComputersTextFile.ReadLine
    x = x + 1
    Redim Preserve computerArray(x)
Loop

x = 0

softwareArray = Split(objComponentsTextFile.ReadLine, ",")

For Each computer in computerArray
    Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & computer & "\root\cimv2")
    StrOutput = "Uninstalled the following Software:- "
    For Each software in softwareArray
        Set colSoftware = objWMIService.ExecQuery ("Select * from Win32_Product Where Name = " & Chr(34) & software & Chr(34) )
        objSoftware.Uninstall()
        strOutput = strOutput & vbCrLf & objsoftware.Name 
    Next
    WScript.Echo strOutput
Next

这会将文本文件中的数据加载到数组中,然后在数组上循环。我认为您之前遇到过文本流到达 AtTheEndOfStream 并且没有来回传输的问题,这可以解释为什么它只处理第一台计算机。

【讨论】:

  • 对,我想摆脱输入框。但是,我似乎无法让它在 Computer_Names 中取第一行,通过卸载运行它,然后取下一行,运行它......直到最后。
  • 获取第 5 行,字符 1。无效的过程调用或参数。如果太麻烦,请不要担心,我很可能会以比我希望的更手动的方式来做这件事,因为我自己的编码技能还很初级。
  • 在我的截图中没有定义 ForReading 常量。改为 1。应该管用。我不能在这里完全复制它,所以我没有要安装 ATM 的软件。
  • 几乎出现了。它不会引发错误,但它仅适用于列表中的第一台计算机。它确实在最后正确弹出回声,说它卸载了程序,我点击OK,它弹出第二个,并说它没有卸载任何东西。我认为这可能是第二台目标计算机上的 WMI 问题,但我在 Computers_List.txt 中交换了它们,它再次在第一台计算机上工作。
  • 你有足够的耐心吗?查看 wscript.exe 进程,看看它做了什么。显然,查询产品很慢sdmsoftware.com/group-policy-blog/wmi/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-03
  • 1970-01-01
  • 2011-10-06
  • 2017-04-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多