Powershell脚本,用于搜索多个远程PC的特定文件

时间:2016-03-24 16:48:08

标签: powershell

我正在尝试从我的电脑上运行一个PowerShell脚本来对照包含许多远程PC的txt文件。我想针对那些将在特定文件路径中搜索特定文件名和/或文件扩展名的PC列表运行一个脚本。

我一直在尝试为每个循环执行一次操作,并将txt文件作为变量插入。我也试图通过cmdlet“Get-ChildItem”将文件名返回给我。

到目前为止,我还没有能够正常工作。有人能指出我正确的方向吗?以下是我迄今为止尝试的几件事......

**

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

*

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 个答案:

答案 0 :(得分:0)

Get-ChildItem cmdlet中的-filter参数错误。下一个代码片段应该可以工作:

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