Announcement

Collapse
No announcement yet.

Data acquiring script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Data acquiring script

    Hey, guys and girls.

    I wrote a script to harvest some hardware info from my computers all around the place (some 250 stations).
    I would like to hear your input on it:
    Code:
    '
    '
    '
    '
    '
    
    On Error Resume Next
    
    Const FOR_READING = 1
    Const FOR_WRITING = 2
    Const FOR_APPENDING = 8
    
    Const HARD_DISK = 3
    
    
    strComputer = "."
    dataArr =""
    delim = ","
    
    
    
    ' Computer name and model
    set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
    For Each objItem In colItems
       dataArr = dataArr & objItem.Name & delim & objItem.Model & delim
    '  objFile.Write objItem.Name & ","
    '  objFile.Write objItem.Model & ","
    Next
    
    
    ' Processor
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set colCSItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
    Set colProcessors = objWMIService.ExecQuery("Select * from Win32_Processor")
    For Each objProcessor in colProcessors
    '  objFile.Write objProcessor.Name & ","
       dataArr = dataArr & objProcessor.Name & delim
    Next
    
    
    ' Memory
    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set colOSItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
    For Each objOSItem In colOSItems
    '  objFile.Write Round((objOSItem.FreePhysicalMemory / 1024), 2) & ","
    '  objFile.Write Round((objOSItem.TotalVisibleMemorySize / 1024), 2) & ","
    dataArr = dataArr & Round((objOSItem.FreePhysicalMemory / 1024), 2) & delim &_
              Round((objOSItem.TotalVisibleMemorySize / 1024), 2) & delim
    Next
    
    
    ' Disks
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    
    Set colDisks = objWMIService.ExecQuery _
        ("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & "")
    
    For Each objDisk in colDisks
    '    objFile.Write "[" & objDisk.DeviceID & "]" & " - " & Round((objDisk.FreeSpace / (1024*1024)),0) & "   "
    dataArr = dataArr & "[" & objDisk.DeviceID & "]" & " - " & Round((objDisk.FreeSpace / (1024*1024)),0) & delim
    Next
    'objFile.Write ","
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile _
     ("\\[server]\[share]\compdata.csv", FOR_APPENDING, True)
    
    objFile.Write vbCrLf & dataArr 
    
    objFile.Close
    TIA.

    Sorin Solomon


    In order to succeed, your desire for success should be greater than your fear of failure.
    -

  • #2
    Re: Data acquiring script

    nice script Sorin.

    Although the line:
    Set colCSItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
    in the ' Processor section has no pupose there.

    There is no need to make multiple calls to the WMI service (nothing is changed) I would only do this once at the top of the script;
    Code:
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!" _
        & "\\" & strComputer & "\root\cimv2")
    What I also would do is Trim() the text strings, because some of then have unnessesary spaces in front or at the end of the text. And a good practize is to put text in cvs-format between quotes to avoid a mistaken separator. (the quotes are not shown when you open the file with Excel)

    example:
    Code:
    strComputer = "."
    dataArr =""
    delim = ","
    
    Set objWMIService = GetObject("winmgmts:" _
        & "{impersonationLevel=impersonate}!" _
        & "\\" & strComputer & "\root\cimv2")
    
    ' Computer name and model
    Set colItems = objWMIService.ExecQuery( _
       "SELECT * FROM Win32_ComputerSystem")
    For Each objItem In colItems
       dataArr = dataArr & _
         chr(34) & Trim(objItem.Name) & chr(34) & delim _
         & chr(34) & Trim(objItem.Model) & chr(34) & delim
    Next
    
    ' Processor
    Set colProcessors = objWMIService.ExecQuery( _
       "Select * from Win32_Processor")
    For Each objProcessor in colProcessors
       dataArr = dataArr & _
         chr(34) & Trim(objProcessor.Name) & chr(34) & delim
    Next
    
    <......>
    optional;
    If you want to get rid of the empty first line, then fill an array instead of expanding a string.


    \Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: Data acquiring script

      10nx, Rems.

      Sorin Solomon


      In order to succeed, your desire for success should be greater than your fear of failure.
      -

      Comment

      Working...
      X