Home > Software engineering >  Export-CSV is not populating separate CSV files based on source
Export-CSV is not populating separate CSV files based on source

Time:01-10

Good morning,

Hopefully this will be a quick and easy one to answer.

I am trying to run a PS script and have it export to csv based on a list of IP addresses from a text file. At the moment, it will run but only produce one csv.

Code Revision 1

$computers = get-content "pathway.txt"
$source = "\\$computer\c$"

foreach ($computer in $computers) {
   
   Get-ChildItem -Path "\\$Source\c$" -Recurse -Force -ErrorAction SilentlyContinue |
   Select-Object Name,Extension,FullName,CreationTime,LastAccessTime,LastWriteTime,Length |
   
Export-CSV -Path "C:\path\$computer.csv" -NoTypeInformation
}

Edit

The script is now creating the individual server files as needed and I did change the source .txt file to list the servers by HostName rather than IP. The issue now is that no data is populating in the .csv files. It will create them but nothing populates. I have tried different source file paths to see if maybe its due to folder permissions or just empty but nothing seems to populate in the files.

The $computer file lists a number of server IP addresses so the script should run against each IP and then write out to a separate csv file with the results, naming the csv file the individual IP address accordingly.

Does anyone see any errors in the script that I provided, that would prevent it from writing out to a separate csv with each run? I feel like it has something to do with the foreach loop but I cannot seem to isolate where I am going wrong.

Also, I cannot use any third-party software as this is a closed network with very strict FW rules so I am left with powershell (which is okay). And yes this will be a very long run for each of the servers but I am okay with that.

Edit I did forget to mention that when I run the script, I get an error indicating that the export-csv path is too long which doesn't make any sense unless it is trying to write all of the IP addresses to a single name.

"Export-CSV : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 characters, and the directory name must be less than 248 characters. At line:14 char:1

TIA

CodePudding user response:

Running the script against C: Drive of each computer is strongly not advisable that too with Recurse option. But for your understanding, this is how you should pass the values to the variables. I haven't tested this code.

$computer = get-content "pathway.txt"
foreach ($Source in $computer) {
   
   Get-ChildItem -Path "\\$Source\c$" -Recurse -Force -ErrorAction SilentlyContinue |
   Select-Object Name,Extension,FullName,CreationTime,LastAccessTime,LastWriteTime,Length | Export-Csv -Path "C:\Path\$source.csv" -NoTypeInformation
}

$computer will hold the whole content and foreach will loop the content and $source will get one IP at a time. I also suggest instead of IP's you can have hostname so that your output file have servername.csv for each server.

CodePudding user response:

In hopes that this helps someone else. I have finally got the script to run and create the individual .csv files for each server hostname.

$servers = Get-Content "path"

Foreach ($server in $servers)
{
Get-ChildItem -Path "\\$server\c$" -Recurse -Force -ErrorAction SilentlyContinue |

Select-Object Name,Extension,FullName,CreationTime,LastAccessTime,LastWriteTime,Length |

Export-CSV -Path "path\$server.csv" -NoTypeInformation
}
  • Related