powershellserversharenetwork-shares

Search all Server Shares for a wildcard filename


I'm looking for a PowerShell script that will search all shares on a server for a wildcard of *_HELP_instructions*. An example of a files that it would search for would be 12_HELP_INSTRUCTIONS.txt or 22_HELP_INSTRUCTIONS.html.

So far I have the script below that will search the contents of the C:\, but I need a script that I can setup to search server shares instead.

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

Solution

  • Use Get-WmiObject to enumerate the shared folders on the server:

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

    The Where-Object clause excludes the administrative shares from being scanned (otherwise you could simply scan all local drives).

    Then call Get-ChildItem with the resulting list of paths:

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