Gathering Properties from Multiple WMI Objects

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!


November 14th, 2014 9:04am

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

Free Windows Admin Tool Kit Click here and download it now
November 14th, 2014 9:21am

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?
November 14th, 2014 1:18pm

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 15 hours 57 minutes ago
Free Windows Admin Tool Kit Click here and download it now
November 14th, 2014 2:20pm

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
November 14th, 2014 2:40pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics