none
multiple wmi queries - export csv RRS feed

  • Question

  • Hello I am trying to query wmi data and export it to a csv, how ever I cant seem to get multiple wmi data to append to the same file. I don't know why you can't chain the command.

    $computers = Get-Content -Path C:\machines.txt
    $computers = $computers.Trim()
    
    $test = Get-WmiObject win32_Bios -cn $computers | Select-Object Description,SerialNumber 
    
    $test2 = Get-WmiObject Win32_Computersystem -cn $computers | Select-Object Domain,Manufacturer,Model,Name,NumberOfProcessors
    
    $test2 | Export-CSV -Path "C:\Users\Public\outputfile.csv" -notypeinformation
    	
    $test | Export-CSV -Path "C:\Users\Public\outputfile.csv" -append -Force

    Thank you for you help.

    Thursday, July 17, 2014 12:23 AM

Answers

  • I think you want to do something like this:


    get-content "C:\machines.txt" | foreach-object {
      $biosInfo = get-wmiobject Win32_BIOS -computername $_
      $computerInfo = get-wmiobject Win32_ComputerSystem -computername $_
      new-object PSObject -property @{
        "ComputerName" = $_
        "Description" = $biosInfo.Description
        "SerialNumber" = $biosInfo.SerialNumber
        "Domain" = $computerInfo.Domain
        "Manufacturer" = $computerInfo.Manufacturer
        "Model" = $computerInfo.Model
        "Name" = $computerInfo.Name
        "NumberOfProcessors" = $computerInfo.NumberOfProcessors
      }
    } | export-csv "C:\Users\Public\outputfile.csv" -notypeinformation
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by justin rassi Thursday, July 17, 2014 1:28 AM
    Thursday, July 17, 2014 12:56 AM
    Moderator

All replies

  • You can not place dissimilar outputs into a CSV file.  An append only works for columns with the same name.

    You need to rethink what you are trying to do.  It makes no sense.


    ¯\_(ツ)_/¯

    Thursday, July 17, 2014 12:26 AM
  • I think you want to do something like this:


    get-content "C:\machines.txt" | foreach-object {
      $biosInfo = get-wmiobject Win32_BIOS -computername $_
      $computerInfo = get-wmiobject Win32_ComputerSystem -computername $_
      new-object PSObject -property @{
        "ComputerName" = $_
        "Description" = $biosInfo.Description
        "SerialNumber" = $biosInfo.SerialNumber
        "Domain" = $computerInfo.Domain
        "Manufacturer" = $computerInfo.Manufacturer
        "Model" = $computerInfo.Model
        "Name" = $computerInfo.Name
        "NumberOfProcessors" = $computerInfo.NumberOfProcessors
      }
    } | export-csv "C:\Users\Public\outputfile.csv" -notypeinformation
    


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by justin rassi Thursday, July 17, 2014 1:28 AM
    Thursday, July 17, 2014 12:56 AM
    Moderator
  • ok first let me say that in IE 11 Win 8.1, hitting the reply button or quote...etc. does nothing. ms you should probably fix that, your site is not compatible with your browser.  now on topic, Bill you are my PS god! that is exactly what i wanted. i was still able to use my variable for the machines path, so it works great thanks.
    • Edited by justin rassi Thursday, July 17, 2014 1:30 AM edit
    Thursday, July 17, 2014 1:28 AM