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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBS/WMI Newbie, help needed...nearly working 1

Status
Not open for further replies.

1ncubu5

Technical User
Aug 16, 2012
2
0
0
GB
Hi all,
Hope this is the correct forum to post this question...
I have created a script to list server names and free disk space, but I must be doing something wrong with the way it stores the results as during the loop it keeps duplicating, e.g.

Name: X

Total Physical Memory: 15 GB


Hard Disk Details

DeviceID: C:
FileSystem: NTFS
FreeSpace: 7 GB
Size: 15 GB

DeviceID: E:
FileSystem: NTFS
FreeSpace: 29 GB
Size: 49 GB


Name: X

Total Physical Memory: 15 GB

Name: Y

Total Physical Memory: 15 GB


Hard Disk Details

DeviceID: C:
FileSystem: NTFS
FreeSpace: 7 GB
Size: 15 GB

DeviceID: E:
FileSystem: NTFS
FreeSpace: 29 GB
Size: 49 GB

DeviceID: C:
FileSystem: NTFS
FreeSpace: 5 GB
Size: 15 GB

DeviceID: E:
FileSystem: NTFS
FreeSpace: 46 GB
Size: 49 GB


Name: X

Total Physical Memory: 15 GB

Name: Y

Total Physical Memory: 15 GB

Name: Z

Name: X

Total Physical Memory: 15 GB


Hard Disk Details

DeviceID: C:
FileSystem: NTFS
FreeSpace: 7 GB
Size: 15 GB

DeviceID: E:
FileSystem: NTFS
FreeSpace: 29 GB
Size: 49 GB


Name: X

Total Physical Memory: 15 GB

Name: Y

Total Physical Memory: 15 GB


Hard Disk Details

DeviceID: C:
FileSystem: NTFS
FreeSpace: 7 GB
Size: 15 GB

DeviceID: E:
FileSystem: NTFS
FreeSpace: 29 GB
Size: 49 GB

DeviceID: C:
FileSystem: NTFS
FreeSpace: 5 GB
Size: 15 GB

DeviceID: E:
FileSystem: NTFS
FreeSpace: 46 GB
Size: 49 GB


Name: X

Total Physical Memory: 15 GB

Name: Y

Total Physical Memory: 15 GB

Name: Z
...etc

I'm sure it's a simple fix for someone... here is the source code.
Code:
comps="c:\apps\servers.txt" 
Servers="c:\apps\servers.htm" 
set fso=createobject("scripting.filesystemobject") 
set objComputersTxt=fso.opentextfile(comps,1) 
set objServersTxt = fso.OpenTextFile(servers,2,True) 
do while not objComputersTxt.atendofstream 
strComputer = objComputersTxt.readline 
Set objWMIService = GetObject("winmgmts:\\" _ 
& strComputer& "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery( _ 
"SELECT * FROM Win32_ComputerSystem") 
For Each objItem in colItems 
    r = r & "Name: " & objItem.Name & "</p>" 
    r = r & "Total Physical Memory: " & Int(objItem.TotalPhysicalMemory /1073741824) & " GB" & "</p>" 
Next 
objServersTxt.WriteLine(r) 
objServersTxt.writeLine("</p>") 
objServersTxt.writeLine("Hard Disk Details" & "</p>") 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk where DeviceID = 'C:' or DeviceID = 'E:'",,48) 
For Each objItem in colItems 
    p = p & "DeviceID: " & objItem.DeviceID & "<br>" 
    p = p & "FileSystem: " & objItem.FileSystem & "<br>" 
    p = p & "FreeSpace: " & Int(objItem.FreeSpace /1073741824) & " GB" & "<br>" 
    p = p & "Size: " & Int(objItem.Size /1073741824) & " GB" & "</p>" 
Next 
objServersTxt.WriteLine(p) 
objServersTxt.writeLine("</p>") 
Loop 
objComputersTxt.close 
objServersTxt.close

Thanks in advance!

 
Clear your variables before setting them with the next server's information
Code:
comps="c:\apps\servers.txt" 
Servers="c:\apps\servers.htm" 
set fso=createobject("scripting.filesystemobject") 
set objComputersTxt=fso.opentextfile(comps,1) 
set objServersTxt = fso.OpenTextFile(servers,2,True) 
do while not objComputersTxt.atendofstream 
   strComputer = objComputersTxt.readline 
   Set objWMIService = GetObject("winmgmts:\\" _ 
      & strComputer& "\root\CIMV2") 
   Set colItems = objWMIService.ExecQuery( _ 
      "SELECT * FROM Win32_ComputerSystem") 
   [highlight]r = ""[/highlight]
   For Each objItem in colItems 
      r = r & "Name: " & objItem.Name & "</p>" 
      r = r & "Total Physical Memory: " & Int(objItem.TotalPhysicalMemory /1073741824) & " GB" & "</p>" 
   Next 
   objServersTxt.WriteLine(r) 
   objServersTxt.writeLine("</p>") 
   objServersTxt.writeLine("Hard Disk Details" & "</p>") 
   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
   Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk where DeviceID = 'C:' or DeviceID = 'E:'",,48) 
   [highlight]p = ""[/highlight]
   For Each objItem in colItems 
      p = p & "DeviceID: " & objItem.DeviceID & "<br>" 
      p = p & "FileSystem: " & objItem.FileSystem & "<br>" 
      p = p & "FreeSpace: " & Int(objItem.FreeSpace /1073741824) & " GB" & "<br>" 
      p = p & "Size: " & Int(objItem.Size /1073741824) & " GB" & "</p>" 
   Next 
   objServersTxt.WriteLine(p) 
   objServersTxt.writeLine("</p>") 
Loop 
objComputersTxt.close 
objServersTxt.close
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top