1

I am attempting to run a powershell script from my PC against a txt file that contains numerous remote PC's. I want to run a script against that list of PC's that will search a certain file path for specific file name and/or file extension.

I have been attempting to do a for each loop with my txt file inserted as a variable. I am also attempting to get the file names returned to me via the cmdlet "Get-ChildItem."

I have not been able to get anything to work correctly so far. Can anyone point me in the right direction with this? Below are a couple of things I have tried so far...

**

PS C:\Windows\system32> $name= gc env:computername
$computers= get-content -path c:\users\person\Desktop\book2.txt
$csvfile = "c:\temp\$name.csv"
foreach ($computer in $computers) {Get-ChildItem -recurse -filter "C:\Users\*.locky"} 
export-csv -filepath $csvfile

**

*

Get-ChildItem : Second path fragment must not be a drive or UNC name.
Parameter name: path2
At line:4 char:36
+ foreach ($computer in $computers) {Get-ChildItem -recurse -filter "C:\Users\*.lo ...
+                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (C:\Windows\system32:String) [Get-ChildItem], Argume 
   ntException
    + FullyQualifiedErrorId : DirArgumentError,Microsoft.PowerShell.Commands.GetChildItemCommand

Export-Csv : A parameter cannot be found that matches parameter name 'filepath'.
At line:5 char:12
+ export-csv -filepath $csvfile
+            ~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.ExportCsvCommand

*

AND

C:\Windows\system32> ForEach ($system in (Get-Content C:\temp\Book2.txt))

if  ($exists in (Test-Path \\$system\c$\Users\*.locky))
{
    Get-Command $exists | fl Path,FileVersion | Out-File c:\temp\results.csv -Append
} 
At line:1 char:53
+ ForEach ($system in (Get-Content C:\temp\Book2.txt))
+                                                     ~
Missing statement body in foreach loop.
At line:3 char:14
+ if  ($exists in (Test-Path \\$system\c$\Users\*.locky))
+              ~~
Unexpected token 'in' in expression or statement.
At line:3 char:14
+ if  ($exists in (Test-Path \\$system\c$\Users\*.locky))
+              ~~
Missing closing ')' after expression in 'if' statement.
At line:3 char:55
+ if  ($exists in (Test-Path \\$system\c$\Users\*.locky))
+                                                       ~
Unexpected token ')' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingForeachStatement
1
  • $name = $env:COMPUTERNAME would be better than $name= gc env:computername
    – phuclv
    Feb 5 at 0:29

2 Answers 2

0

Wrong -filter parameter in Get-ChildItem cmdlet. Next code snippet should work:

foreach ($computer in $computers) {"\\$computer\C$\Users"|`
    Get-ChildItem -recurse -filter "*.locky"}
0

I used this for one of my customers today, and it worked great:

$servers = ("rds01-e","rds01-f","rds01-g")
$report = @()
foreach ($server in $servers) {
    $files = Get-ChildItem -Path "\\$server\c$\Program Files (x86)\Microsoft Office\root\Office16\Library\*.xlam"
    $info = "" | Select ServerName, ExcelFile
    $info.ServerName = $server
    $info.ExcelFile = $files
    $report+=$info
    }
$report
$report | Export-Csv -Path "c:\temp\yep5.csv" -NoTypeInformation
2
  • You might prefer a hashtable, but depending on the PS version (3+), you can roll up PSCustomObjects into the $report array in a ForEach without needing to instantiate it separately. Some smarts have been baked in so it executes faster.
    – LeeM
    Jun 7, 2023 at 23:05
  • e.g.: $report = Foreach (...) { $files = GCI (...); [pscustomobject]@{ Servername = $server; Excelfile = $files} } For some objects, you might need to assign a variable name to the PSCustomObject and output that alone before the end of the foreach loop, but it's generally not neded. $report = foreach (...) { $files = ...; $i = [pscustomobject]@{...} ; $i }
    – LeeM
    Jun 7, 2023 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.