none
WMI to export computer info and still list computers that return errors

    Question

  • I'm trying to create a WMI query to pull the Computer name, manufacturer, model, System Type, and Username but what I've made doesn't quite give me the right output. If I do from a batch script, it works, but the output doesn't include any computers that were unreachable.

    I tried modifying a script I made for pulling AD info that outputs in the way I need it, but it's not quite right...

    See below for the PowerShell and batch versions I made.

    Batch:

    wmic /node:@d:\input\getmodels.txt /output:"D:\output\listmodels_go.csv" computersystem get model,name,manufacturer,systemtype,Username /format:csv


    PowerShell:

    $Missing = New-Object psobject
      $Missing | Add-Member Noteproperty name("No Data")
      $Missing | Add-Member Noteproperty manufacturer("No Data")
      $Missing | Add-Member Noteproperty model("No Data")
      $Missing | Add-Member Noteproperty SystemType("No Data")
      $Missing | Add-Member Noteproperty Username("No Data")
    
    $Computers = Import-Csv C:\Powershell\WMIQueryInput.csv
    $CurrentDate = Get-Date
    $CurrentDate = $CurrentDate.ToString('MM-dd-yyyy')
    
    ForEach ($Computer in $Computers) {
      $DeviceName = $Computer.Name
    
      Get-WmiObject Win32_Computersystem -ComputerName $DeviceName | Format-List Name,manufacturer,model,SystemType,Username | Select Name,manufacturer,model,SystemType,Username
    
     If($Computer -ne $null){
           $Computer | Export-Csv -Path "C:\Powershell\Logs\WMIQueryOutput.$CurrentDate.csv" -Append
        }else{
           $Missing.name = $DeviceName
           $Missing | Export-Csv -Path "C:\Powershell\Logs\WMIQueryOutput.$CurrentDate.csv" -Append
       }
    }


    • Edited by FFwarriorz Friday, February 09, 2018 6:03 PM wrong output file
    Friday, February 09, 2018 5:44 PM

Answers

  • $template = @{
        name = ''
        manufacturer = ''
        model = ''
        SystemType = ''
        Username = ''
    Status = 'OffLine' } $Computers = Import-Csv C:\Powershell\WMIQueryInput.csv $CurrentDate = Get-Date $CurrentDate = $CurrentDate.ToString('MM-dd-yyyy') $Computers | ForEach-Object{ $hash = $template.Clone() $hash.name = $_.Name if ($system = Get-WmiObject Win32_Computersystem -ComputerName $_.Name) {
    $hash.Status = 'Online' $hash.manufacturer = $system.Manufacturer $hash.model = $system.model $hash.systemType = $system.SystemType } [pscustomobject]$hash } | Export-Csv "C:\Powershell\Logs\EmployeeNumbers.$CurrentDate.csv"


    \_(ツ)_/



    • Edited by jrvModerator Friday, February 09, 2018 6:01 PM
    • Marked as answer by FFwarriorz Friday, February 09, 2018 6:04 PM
    Friday, February 09, 2018 6:00 PM
    Moderator

All replies

  • $template = @{
        name = ''
        manufacturer = ''
        model = ''
        SystemType = ''
        Username = ''
    Status = 'OffLine' } $Computers = Import-Csv C:\Powershell\WMIQueryInput.csv $CurrentDate = Get-Date $CurrentDate = $CurrentDate.ToString('MM-dd-yyyy') $Computers | ForEach-Object{ $hash = $template.Clone() $hash.name = $_.Name if ($system = Get-WmiObject Win32_Computersystem -ComputerName $_.Name) {
    $hash.Status = 'Online' $hash.manufacturer = $system.Manufacturer $hash.model = $system.model $hash.systemType = $system.SystemType } [pscustomobject]$hash } | Export-Csv "C:\Powershell\Logs\EmployeeNumbers.$CurrentDate.csv"


    \_(ツ)_/



    • Edited by jrvModerator Friday, February 09, 2018 6:01 PM
    • Marked as answer by FFwarriorz Friday, February 09, 2018 6:04 PM
    Friday, February 09, 2018 6:00 PM
    Moderator
  • How are you such a genius at this? It amazes me every time.
    Friday, February 09, 2018 6:04 PM
  • Hey. Don't blame me.  I just read the manual cover-to-cover and did all of the examples and exercises.

    \_(ツ)_/

    Friday, February 09, 2018 6:06 PM
    Moderator
  • Haha, I didn't know there WAS a manual...

    Thanks again!

    Friday, February 09, 2018 6:14 PM
  • The simple one is to type, as in Unix, "man man" and see what you get.


    \_(ツ)_/

    Friday, February 09, 2018 6:22 PM
    Moderator