Avatar of rstorm1
rstorm1
Flag for United States of America asked on

Powershell Export-CSV only gives one result

Working with Powershell to pull computer name, manufacturer, and model from a list of computers using Get-WMIObject.  The script works in the Powershell window, but I can't get more than one result (the last computer in the list) in the output when piping to Export-CSV.   I understand that Powershell is probably overwriting the information until it reaches the end of the computer list, but don't know how to fix this.  Does someone have a concise solution on how to get this to work?
Here is what I have so far:

$a = Get-Content "C:\temp\Names.txt"
foreach ($i in $a)
    {$i + "`n" + "=========================="; Get-WMIObject Win32_ComputerSystem -computername $i | Select-Object Name, Manufacturer, Model | Export-CSV "C:\temp\MakeModel.csv"}
Powershell

Avatar of undefined
Last Comment
becraig

8/22/2022 - Mon
becraig

try placing the export-csv outside the foreach loop.


$a = Get-Content "C:\temp\Names.txt"
foreach ($i in $a)
    {$i + "`n" + "=========================="; Get-WMIObject Win32_ComputerSystem -computername $i | Select-Object Name, Manufacturer, Model}  | Export-CSV "C:\temp\MakeModel.csv"

Open in new window

becraig

This should also give you what you need based on your current code:

$a = Get-Content "C:\temp\Names.txt"
[array]$Details=$null
foreach ($i in $a)
    {   $details += Get-WMIObject Win32_ComputerSystem -computername $i | Select-Object Name, Manufacturer, Model
} $details | select * | Export-CSV "C:\temp\MakeModel.csv"

Open in new window

becraig

Made one edit to remove the type information:

$a = Get-Content "C:\temp\Names.txt"
[array]$Details=$null
foreach ($i in $a)
    {$details += Get-WMIObject Win32_ComputerSystem -computername $i | Select-Object Name, Manufacturer, Model
} $details | select * | Export-CSV "C:\temp\MakeModel.csv" -nti

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
rstorm1

ASKER
Thanks, becraig, the first solution gives me the following error:
The empty pipe element is not allowed.

The second solution gives the following errors:
Get-WMIObject : Cannot validate argument on parameter 'ComputerName'.  The argument is null or empty
Get-WMMIObject : Not found
Get-WMIObject : The RPC server is unavailable. <Exeption from HRESULT: 0x800706BA>
ASKER CERTIFIED SOLUTION
becraig

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
rstorm1

ASKER
Apologies, becraig, it is working; the errors that I was getting are from a handful of computers in the list that were being queried.  So, I imagine they either have some type of WMI problem or they are offline.
The last one you sent works great.
Thank you very much for your help!
rstorm1

ASKER
becraig is awesome!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
becraig

Glad to help.