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."   
Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
Groveham
  • 103
  • 1
  • 1
  • 8

3 Answers3

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
Benjamin Hubbard
  • 2,797
  • 22
  • 28
  • 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." – Groveham Dec 08 '15 at 15:29
  • You must be using v2. I would recommend then storing the entire foreach loop into $Output and exporting that instead. – Benjamin Hubbard Dec 08 '15 at 16:26
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
} 
Jose Ortiz
  • 705
  • 1
  • 9
  • 19
1

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 
}  
bentek
  • 235
  • 1
  • 9