Link to home
Start Free TrialLog in
Avatar of detox1978
detox1978Flag for United Kingdom of Great Britain and Northern Ireland

asked on

VBScript: Loop through computers.txt instead of strComputer = "."

Hi All,

The script below writes WMI info on a PC into a CSV file.

Could someone help edit it, so it loops through a text file computers.txt instead of being a single PC

On Error Resume Next

Set objFSO=CreateObject("Scripting.FileSystemObject")

outFile="output.csv"
Set objFile = objFSO.CreateTextFile(outFile,True)

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)


For Each objItem in colItems
    objFile.Write "Caption: " & objItem.Caption & ","
    objFile.Write "Description: " & objItem.Description & ","
    objFile.Write "Domain: " & objItem.Domain & ","
    objFile.Write "Manufacturer: " & objItem.Manufacturer & ","
    objFile.Write "Model: " & objItem.Model & ","
    objFile.Write "Name: " & objItem.Name & ","
    objFile.Write "NumberOfProcessors: " & objItem.NumberOfProcessors & ","
    objFile.Write "SystemType: " & objItem.SystemType & ","
    objFile.Write "TotalPhysicalMemory: " & objItem.TotalPhysicalMemory & ","
    objFile.Write "UserName: " & objItem.UserName & ","
Next

objFile.Close

Open in new window


Many thanks
D
Avatar of Rgonzo1971
Rgonzo1971

Hi,

pls try
On Error Resume Next

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set Names = objFSO.OpenTextFile("C:\computers.txt", ForReading)

outFile = "output.csv"
Set objFile = objFSO.CreateTextFile(outFile, True)
Do Until Names.AtEndOfStream
    strComputer = Names.ReadLine
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem", , 48)
    
    
    For Each objItem In colItems
        objFile.Write "Caption: " & objItem.Caption & ","
        objFile.Write "Description: " & objItem.Description & ","
        objFile.Write "Domain: " & objItem.Domain & ","
        objFile.Write "Manufacturer: " & objItem.Manufacturer & ","
        objFile.Write "Model: " & objItem.Model & ","
        objFile.Write "Name: " & objItem.Name & ","
        objFile.Write "NumberOfProcessors: " & objItem.NumberOfProcessors & ","
        objFile.Write "SystemType: " & objItem.SystemType & ","
        objFile.Write "TotalPhysicalMemory: " & objItem.TotalPhysicalMemory & ","
        objFile.Write "UserName: " & objItem.UserName & ","
    Next
Loop
objFile.Close

Open in new window

Regards
Avatar of detox1978

ASKER

Thanks,

When I run the script it creates the CSV file, but not data.  It just seems to hang.

Any suggestions?
Are you running as the domain admin? A normal user can't just suddenly access all information of any random computer.
If I run it as a domain admin it only returns one row?

My script can be run without domain admin rights, as it's just looking at WMI
Any suggestions?
Add this at the top and it should work
Const ForReading = 1

Open in new window


Works even better if you change the Write to WriteLine.

You are suggesting that WMI releases info to guest connection, this is not correct. I suggest you test it as domain admin (it works, output file will be filled up), and try again on a normal PC as a guest or normal user (doesn't work, output file will be mostly empty).
Obviously, if you lowered WMI rights from the default, I'd say you opened up a security hole all by yourself.

Also note the line On Error Resume Next
This will cause offline computer to have the labels from the previous WMI request.
If you have PC1 PC2 and PC3, and you run the script when PC3 is offline, you will have PC3 reported with the info of PC2. Quite dangerous if you ask me. Well, not dangerous literally, but maybe if you depend a next budget meeting on this info, you may be replacing good hardware with new hardware or you skipped old hardware that won't be replaced due to the budget meeting not reflecting the need.
Struggling to get this to work.

Could you put the whole script together and I'll run it as a domain admin.
Also, how do I get it to skip or write offline?
ASKER CERTIFIED SOLUTION
Avatar of Kimputer
Kimputer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Worked great thanks.