搜索所有服务器共享以获取通配符文件名



我正在寻找一个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
}

使用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

您可以使用ForEach Loop循环浏览您需要搜索的所有路径。

$paths = Get-Content C:TempPathlist.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:TempResult.csv -NoTypeInformation -Append
   } else {
      Write-Host "System Not Infected!" 
      }
}

这将遍历C:TempPathList.txt文件中的每个$path。(例如C:*\ServerNameMyShare*

然后将输出推到C:TempResult.csv。如果系统被感染。

这将需要一段时间才能运行,具体取决于您在TXT文件中输入多少路径。但这将实现您的目标!

希望这会有所帮助!

非常感谢您的反馈。考虑到所有调解的最终代码如下:

$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
}

最新更新