質問

I am looking to turn a single computer script into a mass use script. What it does as it stands is allow me to select which parts of Windows Essentials to uninstall by pulling from a text file. However, as it sits, it just prompts for a SINGLE computer name...I want it to pull from a separate text file that I have created Computer_Names.txt, and run the uninstall on all computers in that list. I just can't seem to get it correct.

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

Content of the Computer_Names.txt file would just be something like this. Each computer on a separate line of text.

LAPD2712
LAPD2812

All our computer names are 4 upper-case letters, 4 numbers.

役に立ちましたか?

解決

You could do something like this:

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

This loads the data from your text files into arrays then loops on the arrays. I think you previously had an issue with a text stream getting at the AtTheEndOfStream and not going back and forth, which would explain why it'd process only the first computer.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top