Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-10-26
10
Medium Priority
?
72 Views
Last Modified: 2016-10-27
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
0
Comment
Question by:detox1978
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
10 Comments
 
LVL 53

Expert Comment

by:Rgonzo1971
ID: 41860490
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
0
 
LVL 2

Author Comment

by:detox1978
ID: 41860584
Thanks,

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

Any suggestions?
0
 
LVL 36

Expert Comment

by:Kimputer
ID: 41860595
Are you running as the domain admin? A normal user can't just suddenly access all information of any random computer.
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
LVL 2

Author Comment

by:detox1978
ID: 41860627
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
0
 
LVL 2

Author Comment

by:detox1978
ID: 41860948
Any suggestions?
0
 
LVL 36

Expert Comment

by:Kimputer
ID: 41861768
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.
0
 
LVL 2

Author Comment

by:detox1978
ID: 41861815
Struggling to get this to work.

Could you put the whole script together and I'll run it as a domain admin.
0
 
LVL 2

Author Comment

by:detox1978
ID: 41861823
Also, how do I get it to skip or write offline?
0
 
LVL 36

Accepted Solution

by:
Kimputer earned 2000 total points
ID: 41861910
CONST ForReading = 1

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)
    
	If Err.Number = 0 Then
		For Each objItem In colItems
			objFile.WriteLine "Caption: " & objItem.Caption & ","
			objFile.WriteLine "Description: " & objItem.Description & ","
			objFile.WriteLine "Domain: " & objItem.Domain & ","
			objFile.WriteLine "Manufacturer: " & objItem.Manufacturer & ","
			objFile.WriteLine "Model: " & objItem.Model & ","
			objFile.WriteLine "Name: " & objItem.Name & ","
			objFile.WriteLine "NumberOfProcessors: " & objItem.NumberOfProcessors & ","
			objFile.WriteLine "SystemType: " & objItem.SystemType & ","
			objFile.WriteLine "TotalPhysicalMemory: " & objItem.TotalPhysicalMemory & ","
			objFile.WriteLine "UserName: " & objItem.UserName & ","
		Next
	Else
		objFile.WriteLine "Skipped " & strComputer
		Err.Clear
	End If
Loop
objFile.Close

Open in new window

0
 
LVL 2

Author Closing Comment

by:detox1978
ID: 41863155
Worked great thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Uncontrolled local administrators groups within any organization pose a huge security risk. Because these groups are locally managed it becomes difficult to audit and maintain them.
For anyone that has accidentally used newSID with Server 2008 R2 (like I did) and hasn't been able to get the server running again because you were unlucky (as I was) and had no backups - I was able to get things working by doing a Registry Hive rec…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question