0

I am running the script below, which looks for .js files on a list of devices but am struggling with exporting the results.

The csv file is created but contains no content and I know there should be plenty of results to be included. I have tried various options with select and export-csv & out-file. What I need in the csv is the filename, location and the device it was found on.

This is my latest code:

$servers = Get-Content -Path C:\servers.txt
$report = @()
foreach ($server in $servers) 
{
    $info = Invoke-Command -computername $server -ScriptBlock {
        Get-ChildItem "*.js" -Recurse | Where-Object { $_.CreationTime -ge "05/07/2023" -and $_.CreationTime -le "11/07/2023"} | Select ServerName, Name 
    }
} 
$report | Export-Csv -Path "c:\results.csv" -NoTypeInformation

Update I have emended the code to below and the CSV file now contains the computer names but no file results

$servers = Get-Content -Path C:\servers.txt
$report = @()
foreach ($server in $servers) 
{
    $files = Invoke-Command -computername $server -ScriptBlock {
        Get-ChildItem "*.js" -Recurse | Where-Object { $_.CreationTime -ge "05/07/2023" -and $_.CreationTime -le "11/07/2023"} | Select-Object -ExpandProperty VersionInfo
    }
    $info = "" | Select ServerName, Name
    $info.ServerName = $server
    $info.Name = $files
    $report+=$info
}
$report 
$report | Export-Csv -Path "c:\results.csv" -NoTypeInformation

2nd update This is the code which worked for me, could be refined more but at least it gets the basics into the csv

$servers = Get-Content -Path C:\servers.txt
$report = @()
foreach ($server in $servers) 
{
    $files = Invoke-Command -computername $server -ScriptBlock {
      Get-ChildItem -Path "C:\" -Filter "*.js" -Recurse | Where-Object { $_.CreationTime -ge "05/07/2023" -and $_.CreationTime -le "11/07/2023"} | Select-Object -ExpandProperty VersionInfo
    }
    $report+=$files
}
$report 
$report | Export-Csv -Path "c:\results.csv" -NoTypeInformation
Wychrit
  • 3
  • 2
  • 1
    Looks like you removed the `$report+=$info` statement that used to be after the `Invoke-Command` statement :) – Mathias R. Jessen Jul 12 '23 at 13:24
  • 1
    As an aside: Extending arrays in a loop with `+=` is inefficient, because a _new_ array must be created behind the scenes _in every iteration_, given that arrays are of fixed size; a much more efficient approach is to use a `foreach` loop as an _expression_ and let PowerShell itself collect the outputs in an array: `[array] $outputs = foreach (...) { ... }` - see [this answer](https://stackoverflow.com/a/60708579/45375). In case you need to create arrays manually, e.g. to create _multiple_ ones, use an efficiently extensible list type - see [here](https://stackoverflow.com/a/60029146/45375). – mklement0 Jul 12 '23 at 13:25
  • 1
    As an aside: `Invoke-Command` accepts an _array_ of computer names to target _in parallel_, which is much faster than targeting each computer _in sequence_. While the output order isn't guaranteed to reflect the order of the given computer names, the output objects are decorated with properties that reflect the computer of origin, notably the `.PSComputerName` property. See [this answer](https://stackoverflow.com/a/71773797/45375) for details. – mklement0 Jul 12 '23 at 13:31
  • In this case where should the $report+=$info statement be placed? – Wychrit Jul 12 '23 at 13:43
  • 1
    [Where it was](https://stackoverflow.com/questions/76669783/using-powershell-to-search-a-list-of-devices-for-files-with-a-certain-extension/76669813#76669813) – Mathias R. Jessen Jul 12 '23 at 13:46
  • Sadly its still coming up blank with it there – Wychrit Jul 12 '23 at 13:56
  • 1
    Try `Get-ChildItem -Path "C:\" -Filter "*.js" -Recurse` It appears you're wanting to check the whole server. Think it's failing trying to Recurse `-Path C:\*.js`. if you had any JS files in root C: it would probably return them. – Grok42 Jul 12 '23 at 14:30
  • That's has helped, I made a couple of other tweaks and now get all the files outputted along with the computername, its not pretty but I can live with that. Ill put my latest code in the question. – Wychrit Jul 12 '23 at 14:51

0 Answers0