none
Gathering Properties from Multiple WMI Objects RRS feed

  • Question

  • So what I'm trying to do is pull an inventory of make, model, and serial numbers for a list of machines. I've only just gotten into Powershell as of last week so be easy on me.The problem I'm having is that it's only returning the last machine in the list that I have, if anything. I can verify that all the machines are connected and that I have administrative access to all of them. Here is the code I have:

    $Cred= Get-Credential
    
    get-content targets.txt | foreach-object{
        $Make = Get-WmiObject Win32_Computersystem -Computername $_ -Credential $Cred
        $Serial = Get-WmiObject Win32_BIOS -Computername $_ -Credential $Cred
           Select-Object $($Make).Name,$($Make).Manufacturer,$($Make).Model,$($Serial).SerialNumber
            } | Export-Csv Results.csv -NoTypeInformation

    I also seem to have a problem with not getting a response back from some machines so the script hangs indefinitely and I'm not sure of a way to get around this other than stopping and restarting after removing that machine. Any help would be GREATLY appreciated!


    • Edited by McCoid1017 Friday, November 14, 2014 2:20 PM
    Friday, November 14, 2014 1:58 PM

Answers

  • Either build a hash table of the results and create a custom object or add the -append paramter to export-csv, at the moment you're just overwriting each previous result with the next.
    • Marked as answer by McCoid1017 Friday, November 14, 2014 7:35 PM
    Friday, November 14, 2014 7:14 PM

All replies

  • Are the machines causing this script to hang offline? You can test to make sure they are online before you try to connect 

    $Cred= Get-Credential
    
    get-content targets.txt | foreach-object{
    
    if (test-connection $_ -count 1 -quiet) {
        $Make = Get-WmiObject Win32_Computersystem -Computername $_ -Credential $Cred
        $Serial = Get-WmiObject Win32_BIOS -Computername $_ -Credential $Cred
           Select-Object $($Make).Name,$($Make).Manufacturer,$($Make).Model,$($Serial).SerialNumber
            }} | Export-Csv Results.csv -NoTypeInformation

    Friday, November 14, 2014 2:16 PM
  • I'm actually not sure what's causing this. It seems that the machines that it's hanging on are accessible but potentially something wrong with WMI. So they receive the request but never return anything and the script never moves on. I actually forgot to state initially what my problem actually was. The script is only returning results for the last machine on the list... any idea why?
    Friday, November 14, 2014 6:13 PM
  • Either build a hash table of the results and create a custom object or add the -append paramter to export-csv, at the moment you're just overwriting each previous result with the next.
    • Marked as answer by McCoid1017 Friday, November 14, 2014 7:35 PM
    Friday, November 14, 2014 7:14 PM
  • Seemed to work just fine! Thanks a lot! Sorry for the dumb question...  I wasn't sure if that was a switch or not... Still learning though I guess lol
    Friday, November 14, 2014 7:35 PM