如何让VBS从文本文件中读取?

问题描述:

我正在寻找将单个计算机脚本转换为批量使用脚本。它的功能允许我通过从文本文件中提取来选择卸载Windows Essentials的哪些部分。但是,它正好位于它的前面,它只是提示输入一个单一的计算机名称...我希望它从已创建Computer_Names.txt的单独文本文件中提取,然后在该列表中的所有计算机上运行卸载。我似乎无法弄清楚它的正确性。如何让VBS从文本文件中读取?

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个数字。

+0

请发布文本文件的内容。 –

+0

刚刚编辑主要帖子来显示。 – user3100777

你可以做这样的事情:

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 

这从文本文件加载数据到数组,然后循环的阵列。我认为你以前遇到过一个文本流问题,而不是来回访问,这就解释了为什么它只处理第一台计算机。

+0

对,我想摆脱输入框。然而,我似乎无法得到它在Computer_Names中的第一行,通过卸载运行它,然后采取下一行,运行它......直到结束。 – user3100777

+0

获取第5行,char 1.无效的过程调用或参数。如果太麻烦了,不要担心,因为我自己的编码技能相当简陋,所以我可能最终会以比我想要的更手动的方式来完成此操作。 – user3100777

+0

没有在剪切中定义的ForReading常量。改为1。应该管用。我不能在这里完全复制它,所以我没有软件我想要安装ATM。 –