Link to home
Create AccountLog in
Avatar of lucianst
lucianst

asked on

Export WMI data to csv file, multiple classes on the same csv line

I am running a script on network to get system data on csv file. I want to obtain a row with all data I need about a computer. But I am not able to obtain data from multiple classes on the same line. Could some guide me. Here is the first and the second script I use.

FIRST SCRIPT
On Error Resume Next

Dim oLogObject
Dim oLogOutput
Dim sLogFile

set oLogOutput = nothing

Dim aArgs
Dim strComputer
Dim objWMIService
Dim colItems
Dim objItem


   strComputer = "."


Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

if err Then
  if err.number = 462 Then
    Wscript.Echo "Error: Could not find a machine named " & strComputer
    Wscript.Quit 2
  else
    WScript.Echo "Error: " & err.number & " - " & err.description
    Wscript.Quit 3
  end if    
end if

sLogFile = "c:\configuratii.csv"

set oLogObject = CreateObject("Scripting.FileSystemObject")

If oLogObject.FileExists(sLogFile) Then
      set oLogOutput = oLogObject.OpenTextFile _
           (sLogFile, 8, True)
Else
      set oLogOutput = oLogObject.CreateTextFile(sLogFile)
            oLogOutput.WriteLine "Date,Name,Number of Processors," & _
    "RAM,Manufacturer,Model,SystemType,Description"
End If

if err Then
  if err.number = 462 Then
    Wscript.Echo "Error: Could not find a machine named " & strComputer
    Wscript.Quit 2
  else
    WScript.Echo "Error: " & err.number & " - " & err.description
    Wscript.Quit 3
  end if    
end if

Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_ComputerSystem",,48)

For Each objItem in colItems

      oLogOutput.WriteLine    Now & "," & _
      """" & objItem.Name & """," & _
      objItem.NumberOfProcessors & "," & _
      objItem.TotalPhysicalMemory & "," & _
      """" & objItem.Manufacturer & """," & _
      """" & objItem.Model & """," & _
      """" & objItem.SystemType & """," & _
      """" & objItem.Description & """"


Next

oLogOutput.Close

wscript.quit (0)


SECOND Script

Option Explicit

On Error Resume Next

Dim oLogObject
Dim oLogOutput
Dim sLogFile

set oLogOutput = nothing

Dim aArgs
Dim strComputer
Dim objWMIService
Dim colItems
Dim objItem


   strComputer = "."


Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

if err Then
  if err.number = 462 Then
    Wscript.Echo "Error: Could not find a machine named " & strComputer
    Wscript.Quit 2
  else
    WScript.Echo "Error: " & err.number & " - " & err.description
    Wscript.Quit 3
  end if    
end if

sLogFile = "c:\configprocs.csv"

set oLogObject = CreateObject("Scripting.FileSystemObject")

If oLogObject.FileExists(sLogFile) Then
      set oLogOutput = oLogObject.OpenTextFile _
           (sLogFile, 8, True)
Else
      set oLogOutput = oLogObject.CreateTextFile(sLogFile)
            oLogOutput.WriteLine "Family,Manufacturer,MaxClockSpeed,Name,OtherFamilyDescription"
End If

Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_Processor",,48)

For Each objItem in colItems

      oLogOutput.WriteLine objItem.Family & "," & _
      """" & objItem.Manufacturer & """," & _
      objItem.MaxClockSpeed & "," & _
      objItem.OtherFamilyDescription &""""


Next

oLogOutput.Close

wscript.quit (0)
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Is there anything else you needed explaining, or did the script not work correctly for you?

Rob.
Avatar of lucianst
lucianst

ASKER

I am obtaining the second class twice now (after I used a string variable). And I have to move to the next line for the next computer on which will the script run (on AD).

Dim oLogObject
Dim oLogOutput
Dim sLogFile
Dim strOutput

set oLogOutput = nothing
set strOutput = nothing

Dim aArgs
Dim strComputer
Dim objWMIService
Dim colItems
Dim objItem


   strComputer = "."


Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")

if err Then
  if err.number = 462 Then
    Wscript.Echo "Error: Could not find a machine named " & strComputer
    Wscript.Quit 2
  else
    WScript.Echo "Error: " & err.number & " - " & err.description
    Wscript.Quit 3
  end if    
end if

sLogFile = "I:\IT assets management2\06 martie\configuratii.csv"

set oLogObject = CreateObject("Scripting.FileSystemObject")

If oLogObject.FileExists(sLogFile) Then
      set oLogOutput = oLogObject.OpenTextFile _
           (sLogFile, 8, True)
Else
      set oLogOutput = oLogObject.CreateTextFile(sLogFile)
            oLogOutput.WriteLine "Date,Name,Number of Processors," & _
    "RAM,Manufacturer,Model,SystemType,Description"
End If

if err Then
  if err.number = 462 Then
    Wscript.Echo "Error: Could not find a machine named " & strComputer
    Wscript.Quit 2
  else
    WScript.Echo "Error: " & err.number & " - " & err.description
    Wscript.Quit 3
  end if    
end if

Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_ComputerSystem",,48)

For Each objItem in colItems

      strOutput = Now & "," & _
      """" & objItem.Name & """," & _
      objItem.NumberOfProcessors & "," & _
      objItem.TotalPhysicalMemory & "," & _
      """" & objItem.Manufacturer & """," & _
      """" & objItem.Model & """," & _
      """" & objItem.SystemType & """," & _
      """" & objItem.Description & ""","


Next

Set colItems = nothing
set objitem = nothing

Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_Processor",,48)

For Each objItem in colItems

      strOutput = strOutput & objItem.Family & "," & _
      """" & objItem.Manufacturer & """," & _
      objItem.MaxClockSpeed & "," & _
      objItem.OtherFamilyDescription &""""


Next

Set colItems = nothing
set objitem = nothing

Set colItems = objWMIService.ExecQuery( _
    "SELECT * FROM Win32_Processor",,48)

For Each objItem in colItems

      strOutput = strOutput & objItem.Family & "," & _
      """" & objItem.Manufacturer & """," & _
      objItem.MaxClockSpeed & "," & _
      objItem.OtherFamilyDescription


Next

oLogOutput.WriteLine strOutput

oLogOutput.Close

wscript.quit (0)
Ok. I used two variables and is ok now.