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