Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ForReading - GetObject Help

Status
Not open for further replies.

sotghalz

Technical User
Apr 11, 2007
57
US
How do I make this run on a list of computers from a text file?

Code:
Const ForAppending = 8
Dim objWMIService, objItem, colItems, strComputer
Dim strDriveType, strDiskSize, strDisk

strComputer = "."

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
    ("\\path\status.txt", ForAppending, True)
Set objWMIService =  GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_LogicalDisk")
For Each objItem in colItems
Select Case objItem.DriveType
Case 1 strDriveType = "Drive could not be determined."
Case 2 strDriveType = "Removable Drive"
Case 3 strDriveType = "Local"
Case 4 strDriveType = "Network" 
Case 5 strDriveType = "Compact" 
Case 6 strDriveType = "RAM" 
Case Else strDriveType = "Drive type Problem."
End Select
      
strDisk = strcomputer& objItem.Name & strDriveType

objTextFile.WriteLine(strDisk)

Next
objTextFile.Close
 
Something like this should work (not tested):
Code:
Const [COLOR=blue]ForReading = 1, [/color]ForAppending = 8
Dim objWMIService, objItem, colItems, strComputer
Dim strDriveType, strDiskSize, strDisk
[COLOR=blue]Dim arrComputers[/color]

[s]strComputer = "."[/s]

Set objFSO = CreateObject("Scripting.FileSystemObject")
[COLOR=blue]Set objTextFile = objFSO.OpenTextFile _
    ("\\path\computers.txt", ForReading)
arrComputers = Split(objTextFile.RealAll, vbCrLf)[/color]

Set objTextFile = objFSO.OpenTextFile _
    ("\\path\status.txt", ForAppending, True)

[COLOR=blue]For Each strComputer in arrComputers[/color]

   Set objWMIService =  GetObject _
      ("winmgmts:\\" & strComputer & "\root\cimv2")
   Set colItems = objWMIService.ExecQuery _
      ("Select * from Win32_LogicalDisk")
   For Each objItem in colItems
      ...
   Next
   objTextFile.Close
[COLOR=blue]Next[/color]
 
Thanks for your help. There was a typo here

arrComputers = Split(objTextFile.RealAll, vbCrLf)

but after some head scratching I figured it out. It works great but, there is one problem. In my list of computers I have my workstation name and a few others for testing. It runs on mine but I get a permission denied 'getobject' error.

Is there a way to run this with administrative privileges?
 
Another issue. I was able to work out the problem listed above but now I'm getting only the information for the first computer in the list and none after.
 
because you have

objTextFile.Close
Next

e.g. you close the log file after the first instance of the For Each Next loop
 
ahhh, in addition to the ReadAll typo, I did misplace the "Next"... sorry about that.
 
Thanks for the help. It works out great. I can move forward with this task.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top