Help/Advice on adding data to Report (remote Get-ChildItem)

Hi Folks,
Hoping for some advice - another set of eyes to tackle an issue. I have a text/CSV file containing details

Server,MasterID,WebsiteName,Rootfolder
ABCD,12234,mysite.com,e:\websites\mysite
EFGH,12331,another.com,e:\websites\another

This file is pulled down to a Automation Server /Powershell server so it can be used to hold details on which remote servers to connect to and get some info about files/folders.

$Alldata = Import-CSV -Path $File
foreach ($item in $allData) {
$session = New-PSSession -ComputerName $item.Server -Credential $creds

$output += Invoke-Command -Session $session `
{Get-ChildItem -File -Path $ite.rootfolder -Recurse 
| Select-Object FullName,@{N='LastUpdated';
Expression={Get-Date -Date $_.LastWriteTime -Uformat "%d-%m-%Y %R"}},@{N='MD5';Expression={(Get-FileHash $_.FullName).Hash}} } 

So this gives me something like:

FullName                             LastUpdated       MD5      PSComputerName RunspaceId 
--------                             -----------       ---      -------------- ---------- 
E:\websites\mysite\www\default.aspx  15-07-2020 14:54  EF165CB4 ABCD           dced3d40
E:\websites\mysite\www\other.aspx    15-07-2020 14:55  EF165CB1 ABCD           dced3d40
E:\websites\another\www\default.aspx 24-07-2020 07:16  4F361B43 EFGH           0d731a90
E:\websites\another\www\and.aspx     24-07-2020 07:16  4F361B42 EFGH           0d731a90

Which is great, BUT - I am wondering how can I get values from the original CSV file added to that?
What I mean is - I need to also have the values from the CSV (ie. Server, MasterId, Websitename added to each beginning line of the output and get rid of the PsComputerName and RunSpaceId)

So the end result I need would be:

Server	MasterID FullName                                          LastUpdated      MD5      
------  -------- --------                                          -----------      ---                                                           
ABCD 	12334    E:\websites\mysite\www\default.aspx               15-07-2020 14:54 EF165CB4 
ABCD    12334    E:\websites\mysite\www\other.aspx                 15-07-2020 14:54 EF165CB1 
EFGH    12331    E:\websites\another\www\default.aspx 	           24-07-2020 07:16 4F361B43
EFGH    12331    E:\websites\another\www\and.aspx                  24-07-2020 07:16 4F361B42 

The aim is to create a text file on the automation server that has that output.

Any ideas/thoughts on best way to approach this?

Give this a try

$Alldata = Import-CSV -Path $File
$output = foreach ($item in $allData){

    $session = New-PSSession -ComputerName $item.Server -Credential $creds

    Invoke-Command -Session $session -ScriptBlock {
        Get-ChildItem -File -Path $using:item.rootfolder -Recurse |
            Select-Object FullName,
                          @{N='LastUpdated';E={Get-Date -Date $_.LastWriteTime -Uformat "%d-%m-%Y %R"}},
                          @{N='MD5';E={(Get-FileHash $_.FullName).Hash}}
    } -HideComputerName |
        Select-Object @{N='Server';E={$item.Server}},
                      @{N='MasterID';E={$item.masterid}},
                      FullName,
                      LastUpdated,
                      MD5
}

Another method to try:

$results = foreach ($item in $allData) {
    $session = New-PSSession -ComputerName $item.Server -Credential $creds
     
    $masterId = $item.MasterId

    $sb = {
        Get-ChildItem -File -Path $ite.rootfolder -Recurse | 
        Select-Object @{Name='ComputerName';Expression={$env:COMPUTERNAME}},
                      @{Name='MasterId';Expression={$using:masterId}},
                      FullName,
                      @{Name='LastUpdated';Expression={Get-Date -Date $_.LastWriteTime -Uformat "%d-%m-%Y %R"}},
                      @{Name='MD5';Expression={(Get-FileHash $_.FullName).Hash}}
    }
    
    Invoke-Command -Session $session $sb
 }

$results

Only thing I would add is the default algorithm for Get-FileHash is SHA-256. If you need MD5, add -Algorithm MD5 to the Get-FileHash command.

Wow! Once again, this forum comes to the rescue. I am always humbled by the willingness of folks to help out ! This internet-thing ain’t all bad ! :wink:

Thank you all for your replies - all is working wonderfully.