Collect reportfiles from servers while having multiple directory subpaths


This one is driving me nuts, maybe someone else have an idea.

I have to collect several reportfiles from a bunch of servers (servernames are stored in a textfile).
Each servers runs a task(called “MBX”), creating a report being stored in a subdirectory.
Problem: Some servers are running two tasks, each report is stored in its own subdirectory.

Directorystructure Server1

C:\Program Files (x86)\SomeNameHere\Reports\MBX1\Scheduled\report1Full190213-1715.csv

Directorystructure Server2

C:\Program Files (x86)\SomeNameHere\Reports\MBX1\Scheduled\report1Full190213-1700.csv
C:\Program Files (x86)\SomeNameHere\Reports\MBX2\Scheduled\report2Full190213-18700.csv

The following script runs fine for servers with only one report/directory, but fails on ones with more then one subdirectory.

$Computers = Get-Content C:\admin\EVMBXSERVERS.txt

$date=get-date -format yyyyMMdd

Foreach ($Computer in $Computers) 

$path='\\' + $Computer + '\c$\Program Files (x86)\SomeNameHere\Reports\'

#Value is : \\ComputerName\c$\Program Files (x86)\SomeNameHere\Reports\

$mbxreportdir=get-childitem $path -filter "MBX*" -Recurse 

#Value is : \\ComputerName\c$\Program Files (x86)\SomeNameHere\Reports\MBX1

#---> Trouble: May contain more than one value, depending on number of subdirectories 

foreach ($Name in $mbxreportdir)

$scheduledpath=Join-Path $path $mbxreportdir 
#Value is : \\ComputerName\c$\Program Files (x86)\SomeNameHere\Reports\*MBX*

Join-Path $scheduledpath -Childpath 'Scheduled' 
#Value is : \\ComputerName\c$\Program Files (x86)\SomeNameHere\Reports\*MBX*\Scheduled

get-childitem $scheduledpath -recurse | Where-Object {$ -match $date} |Get-ChildItem -filter "*.csv"|select -Expandproperty Fullname|Copy-Item -Destination c:\reports



I do not want to start with scripts on any single servers copying the reports TO a central collecting folder (the “Sissy” way). I would like to collect the files from a central server using one script.

Maybe the way I have started is completely wrong, but if so, it would be nice to get your suggestions for making it a better way.






If I got you right you only need something like this:

$Computers = Get-Content C:\admin\EVMBXSERVERS.txt
$date = get-date -format yyyyMMdd

Foreach ($Computer in $Computers) {
$path = ‘\’ + Computer + '\c\Program Files (x86)\SomeNameHere\Reports\MBX*\Scheduled’
$mbxreportlist = get-childitem path -filter '*.csv' | Where-Object { -match $date} | Select-Object -Property FullName

foreach ($report in $mbxreportlist) {
    Copy-Item -Path $report -Destination c:\reports