Powershell Export to CSV from ForEach Loop
Asked Answered
C

3

8

I'm new to Powershell but I've given it my best go. I'm trying to create a script to copy a file to the All Users Desktop of all the XP machines in an Array. The script basically says "If the machine is pingable, copy the file, if not, don't." I then want to export this information into a CSV file for further analysis.

I've set it all but no matter what I do, it will only export the last PC that it ran though. It seems to run through all the PC's (tested with outputting to a txt file) but it will not log all the machines to a CSV. Can anyone give any advise?

$ArrComputers = "PC1", "PC2", "PC3"

foreach ($Computer in $ArrComputers) {
    $Reachable = Test-Connection -Cn $Computer -BufferSize 16 -Count 1 -ea 0 -quiet
    $Output = @()

    #Is the machine reachable?
    if($Reachable)
    {
        #If Yes, copy file
        Copy-Item -Path "\\servername\filelocation" -Destination "\\$Computer\c$\Documents and Settings\All Users\Desktop\filename" 
        $details = "Copied"  
    } 
    else
    {
        #If not, don't copy the file
        $details = "Not Copied"
    }   

    #Store the information from this run into the array  
    $Output =New-Object -TypeName PSObject -Property @{
        SystemName = $Computer
        Reachable = $reachable 
        Result = $details
    } | Select-Object SystemName,Reachable,Result
}

#Output the array to the CSV File
$Output | Export-Csv C:\GPoutput.csv

Write-output "Script has finished. Please check output files."   
Cashmere answered 8/12, 2015 at 15:11 Comment(0)
K
10

The problem is this:

#Store the information from this run into the array  
  $Output =New-Object -TypeName PSObject -Property @{
    SystemName = $Computer
    Reachable = $reachable 
    Result = $details
  } | Select-Object SystemName,Reachable,Result
}  
#Output the array to the CSV File
$Output | Export-Csv C:\GPoutput.csv

Each iteration of your foreach loop saves to $Output. Overwriting what was there previously, i.e., the previous iteration. Which means that only the very last iteration is saved to $Output and exported. Because you are running PowerShell v2, I would recommend saving the entire foreach loop into a variable and exporting that.

$Output = foreach ($Computer in $ArrComputers) {
  New-Object -TypeName PSObject -Property @{
    SystemName = $Computer
    Reachable = $reachable 
    Result = $details
  } | Select-Object SystemName,Reachable,Result
}
$Output | Export-Csv C:\GPoutput.csv
Krystalkrystalle answered 8/12, 2015 at 15:19 Comment(2)
I've tried moving the $Output | Export-CSV C:\GPoutput.csv into the array but again it overwrites itself on every pass as there is no -append allowed. I've also tried saving the entire foreach loop into a variable but this brought back errors: "unexpected token 'in' in expression or statement."Cashmere
You must be using v2. I would recommend then storing the entire foreach loop into $Output and exporting that instead.Krystalkrystalle
W
4

You would want to append the export-csv to add items to the csv file Here is an example

foreach ($item in $ITGlueTest.data)
{
$item.attributes | export-csv C:\organization.csv -Append
} 
Wagner answered 15/2, 2018 at 12:57 Comment(0)
P
2

Here you go. This uses PSCustomObject which enumerates data faster than New-Object. Also appends to the .csv file after each loop so no overwriting of the previous data.

foreach ($Computer in $ArrComputers) {

$Reachable = Test-Connection -Cn $Computer -BufferSize 16 -Count 1 -ea 0 -quiet

#Is the machine reachable?
if($Reachable)
{
#If Yes, copy file
Copy-Item -Path "\\servername\filelocation" -Destination "\\$Computer\c$\Documents and Settings\All Users\Desktop\filename" 
$details = "Copied"  
} 
else
{
#If not, don't copy the file
$details = "Not Copied"
} 
#Store the information from this run into the array  
       [PSCustomObject]@{
       SystemName = $Computer
       Reachable = $reachable 
       Result = $details
       } | Export-Csv C:\yourcsv.csv -notype -Append 
}  
Pulchritudinous answered 8/12, 2015 at 15:41 Comment(2)
This will not work because he is using v2. PSCustomObject and -Append are v3+.Krystalkrystalle
Missed the part where he mentioned XP machines.Pulchritudinous

© 2022 - 2024 — McMap. All rights reserved.