在所有服务器共享中搜索通配符文件名

时间:2016-12-13 11:54:20

标签: powershell server share network-shares

我正在寻找一个PowerShell脚本,它将搜索服务器上的所有共享以获得*_HELP_instructions*的通配符。它将搜索的文件示例为12_HELP_INSTRUCTIONS.txt22_HELP_INSTRUCTIONS.html

到目前为止,我有下面的脚本将搜索C:\的内容,但我需要一个脚本,我可以设置搜索服务器共享。

$FilesToSearch = Get-ChildItem "C:\*" -Recurse -ErrorAction SilentlyContinue |
                 where {$_.Name -like "*_HELP_instructions*"}

if ($FilesToSearch -ne $null) {
    Write-Host "System Infected!" 
    exit 2015
} else {
    Write-Host "System Not Infected!" 
    exit 0
}

3 个答案:

答案 0 :(得分:0)

您可以使用ForEach Loop遍历搜索所需的所有路径。

$paths = Get-Content C:\Temp\Pathlist.txt

Foreach($path in $paths){
   $Filestosearch = Get-ChildItem $path -Recurse -ErrorAction SiltenlyContinue| where {$_.Name -like "*_HELP_instructions*"}
   If($FilesToSearch -ne $null) {
      Write-Host "System Infected!" 
   $Filestosearch | Export-Csv c:\Temp\Result.csv -NoTypeInformation -Append
   } else {
      Write-Host "System Not Infected!" 
      }
}

这将遍历$path文件中的每个C:\Temp\PathList.txt。 (例如C:\*\\ServerName\MyShare\*

然后将输出推送到C:\Temp\Result.csv。如果系统受到感染。

这将需要一段时间才能运行,具体取决于您在txt文件中放置的路径数。但它会实现你的目标!

希望这有帮助!

答案 1 :(得分:0)

使用Get-WmiObject枚举服务器上的共享文件夹:

$exclude = 'Remote Admin', 'Remote IPC', 'Default share'
$shared  = Get-WmiObject Win32_Share |
           Where-Object { $exclude -notcontains $_.Description } |
           Select-Object -Expand Path

Where-Object子句不会扫描管理共享(否则您只能扫描所有本地驱动器)。

然后使用生成的路径列表调用Get-ChildItem

$FilesToSearch = Get-ChildItem $shared -Filter '*_HELP_instructions*' -Recurse -ErrorAction SilentlyContinue

答案 2 :(得分:0)

非常感谢大家的反馈。将所有考虑因素考虑在内,最终代码如下:

$exclude = 'Remote Admin', 'Remote IPC', 'Default share'
$shared  = Get-WmiObject Win32_Share |
       Where-Object { $exclude -notcontains $_.Description } |
       Select-Object -Expand Path

$FilesToSearch = Get-ChildItem $shared -Filter '*_HELP_instructions*' -Recurse -ErrorAction SilentlyContinue

If($FilesToSearch -ne $null) 

{
Write-Host "System Infected!" 
exit 2015
}

else 

{
Write-Host "System Clear!" 
exit 0
}