AutomaShell

  • TwitterTwitter
  • LinkedInLinkedIn
  • RSS FeedRSS Feed
  • Home
  • About
  • Contact
  • Troy
  • December 6, 2012
  • 0

Recursive file search across multiple file servers

I was recently asked to search for multiple terms across multiple file servers for legal discovery reasons. Once these files were located, they needed to copied to another location while retaining the original file structure (e.g. \\server\drive\path1\path2\file.txt). This is actually a fairly easy and straight forward task.

polemically What it does

This script will crawl multiple file servers, and multiple search terms. Once located, these files are copied to another location while retaining the original file structure.

http://fabcare.com/azartnye-igry-vo-francii/ What you need and need to know

  • You will need permissions to access the folders and copy the files
  • Patience!!

Learn it

First off, you need to set your $locations array. This array contains the names of our file servers, AND the disks that we will be searching.

1
$locations = "\\fs1\F$", "\\fs2\B$", "\\fs2\Q$", "\\fs2\G$" # File Servers

We also need to input our search terms. The * on both sides of the term ensure we return a match no matter where the term may pop up within the file name itself.

1
$terms = "*file1*", "*file1*", "*file3*" # Search terms

We’re now ready to start our search. Earlier we created our $locations array, so now we’re going to use a foreach command to search each file server in this array. The write-host is for progress alerts.

1
2
foreach($location in $locations) {
write-host "Crawling $location..." -f green

Within the $location search, we have another search for $term.

1
2
foreach($term in $terms) {
write-host "Searching for the term $term..." -f yellow

Now we have setup our search term from the $locations and $terms array. Let’s execute the search!

1
$files = Get-ChildItem -Path $location -Filter $term -Recurse

This is where patience comes in to play. The Get-ChildItem command may take a considerable amount of time to run, depending on the size of your file server.

Once it is done, we will collect the file information (when it was last updated, where it is, and it’s name). After that, we will create a new directory at our copy location if it does not exist, and finally we will copy the file over to our new location.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
foreach($file in $files) {
if((get-date $file.lastwritetime) -gt (get-date 2010-07-01)) {
$folder = $file.directory
$folder = $folder.name
if(!(test-path "$copylocation\$folder" -erroraction silentlycontinue)) {
$searchlocation = ($location).Replace("\\", "")
$searchterm = ($term).Replace("*", "")
New-Item -ItemType Directory -Path $copylocation\$searchlocation\$searchterm\$folder -erroraction silentlycontinue
}
$path = "$copylocation\$searchlocation\$searchterm\$folder"
$directory = $file.directoryname
$filename = $file.name
Copy-Item "$directory\$filename" "$path\$filename"
}
}

That’s it! The loops will continue until each location, term, and file has been searched and copied over. This worked very well for our purposes, but your mileage may vary..

I hope this is useful to you, and thank you for reading.

Download ZIP

[wpdm_file id=3]

Source Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
########################################
# FileSystemCrawl
# Created by Troy Ward
########################################
 
# Variables
$copylocation = "\\copylocation" # No trailing slash!
$locations = "\\fs1\F$", "\\fs2\B$", "\\fs2\Q$", "\\fs2\G$" # File Servers
$terms = "*file1*", "*file1*", "*file3*" # Search terms
 
foreach($location in $locations) {
write-host "Crawling $location..." -f green
foreach($term in $terms) {
write-host "Searching for the term $term..." -f yellow
$files = Get-ChildItem -Path $location -Filter $term -Recurse
foreach($file in $files) {
if((get-date $file.lastwritetime) -gt (get-date 2010-07-01)) {
$folder = $file.directory
$folder = $folder.name
if(!(test-path "$copylocation\$folder" -erroraction silentlycontinue)) {
$searchlocation = ($location).Replace("\\", "")
$searchterm = ($term).Replace("*", "")
New-Item -ItemType Directory -Path $copylocation\$searchlocation\$searchterm\$folder -erroraction silentlycontinue
}
$path = "$copylocation\$searchlocation\$searchterm\$folder"
$directory = $file.directoryname
$filename = $file.name
Copy-Item "$directory\$filename" "$path\$filename"
}
}
}
}

PowerShell

© Copyright 2012-2018 AutomaShell. Contact me if you have any questions.

  • TwitterTwitter
  • LinkedInLinkedIn
  • RSS FeedRSS Feed
  • Tabs

    • Recent Posts
    • Most Popular
    • Comments
    • Nutanix AOS 5.10 & Prism Central 5.10 releasedNovember 26, 2018
    • Nest VMware ESXi on Nutanix AHVSeptember 18, 2018
    • Nutanix Calm – AWS Setup & Example App DeploymentAugust 15, 2018
    • Nutanix: Add Unprotected VMs to Protection DomainJune 7, 2018
    • Deploy VMware VM’s with PowerCLIOctober 23, 2012
    • Enable Exchange 2010 Online Archive in a mixed environment (Part 1: By OU)December 4, 2012
    • User Termination (Part 2)June 6, 2013
    • Search Active Directory with PowerShell (LDAP)April 29, 2013
    • Rob Duff on:Search Active Directory with PowerShell (LDAP)
    • sean on:Deploy VMware VM’s with PowerCLI
    • Troy on:Deploy VMware VM’s with PowerCLI
    • Niklas Ilves on:Deploy VMware VM’s with PowerCLI
  • Categories

    • AWS
    • Calm
    • Exchange Server
    • General
    • Nutanix
    • PowerCLI
    • PowerShell
    • Uncategorized
    • VMware
    • Windows Server