Saturday, 18 July 2015

Search-NaFileSystem.ps1 (7-Mode)


Note: Formatted for blogger. Copy and paste into a text file and save as Search-NaFileSystem.ps1.

# Search-NaFileSystem.ps1 (7-Mode)
# A program to scan the filesystem for a file/folder, files/folders, or completely list the structure

########################
## PROGRAM PARAMETERS ##
########################

Param(
[String]$7Gsystem,
[String]$Username,
[String]$Password,
[String]$Volume,
[System.Array]$Files,
[System.Array]$Folders
)

"";"+++++ Search-NaFileSystem.ps1 (7DOT) +++++";""

#############################################
## CHECK FOR DATA ONTAP POWERSHELL TOOLKIT ##
#############################################

If(!(Get-Module DataONTAP)){Import-Module DataONTAP -ErrorAction SilentlyContinue}
If(!(Get-Module DataONTAP)){"Cannot find Data ONTAP PowerShell Toolkit - exiting!"; ""; EXIT}

#################################
## 1: ACQUIRE DETAILS FOR SCAN ##
#################################

"<<<<< PART 1/3: ACQUIRE DETAILS FOR SCAN >>>>>";""

## 7GSYSTEM/USERNAME/PASSWORD ##
## ========================== ##

$SecurePass = $null
If($Password){ $SecurePass = $Password | ConvertTo-SecureString -AsPlainText -Force }

">>> Enter Connection Details ";""
If(!$7Gsystem)   { $7Gsystem   = Read-Host "7Gsystem" } else { "7Gsystem  = $7Gsystem"}
If(!$Username)   { $Username   = Read-Host "Username" } else { "Username = $Username" }
If(!$SecurePass) { $SecurePass = Read-Host "Password" -AsSecureString}
""

$Credential = New-Object System.Management.Automation.PsCredential($Username,$SecurePass)
$Connection = Connect-NaController -Name $7Gsystem -Credential $Credential -ErrorAction SilentlyContinue
If(!$Connection){"Unable to connect to $7Gsystem - exiting!"; ""; EXIT}

## ACQUIRE VOLUME TO BE SCANNED DETAILS ##
## ==================================== ##

">>> List of Volumes in $7Gsystem "; ""
[System.Array]$VolumesList = (Get-NaVol).Name
$VolumesList | Foreach { $_ }; ""

If(!$Volume){ $Volume = Read-Host "Select Volume" } else { "Volume to check = $Volume" }
If(!($VolumesList -Contains $Volume)){"Volume $Volume does not exist - exiting!"; ""; EXIT}

"Read-NaDirectory uses /vol/VOLNAME = /vol/$Volume"
$ReadNaDirPath = "/vol/$Volume"

## SELECT FILES ##
## ============ ##

"";">>> Select Files"; ""

If(!$Files){
[String]$FilesTemp = Read-Host "Enter comma separated list of files to check for"; ""
[System.Array]$Files = $FilesTemp.Split(",")
}

[System.Array]$FilesCheck = @() # Need to remove any ""
"Files to check = "
$Files | Foreach {
If($_ -ne ""){
$_; $FilesCheck += $_
}
}
$Files = $FilesCheck

## SELECT FOLDERS ##
## ============== ##

"";">>> Select Folders"; ""

IF(!$Folders){
[String]$FoldersTemp = Read-Host "Enter comma separated list of folders to check for"; ""
[System.Array]$Folders = $FoldersTemp.Split(",")
}

[System.Array]$FoldersCheck = @() # Need to remove any ""
"Folders to check = "
$Folders | Foreach {
If($_ -ne ""){
$_; $FoldersCheck += $_
}
}
$Folders = $FoldersCheck

## RECORDING/SEARCHING STATUS ##
## ========================== ##

"";">>> Recording/Searching Status";""

[Boolean]$RecordingOnly = [Boolean]$SearchingFiles = [Boolean]$SearchingFolders = $FALSE
If( ($Files.Count -eq 0) -and ($Folders.Count -eq 0) ){
$RecordingOnly = $TRUE; "RECORDING ONLY!"
} else {
If($Files.Count -ne 0)  { $SearchingFiles = $TRUE;   "SEARCHING for FILES"}
If($Folders.Count -ne 0){ $SearchingFolders = $TRUE; "SEARCHING for FOLDERS"}
}

#################
## 2: SCANNING ##
#################

"";"<<<<< PART 2/3: SCANNING >>>>>";""

$StartDate = date
">>> START TIME = $StartDate";""

# SCAN THROUGH ALL FOLDERS IN THE VOLUME

[System.Array]$Global:DiscoveredFiles = @()
[System.Array]$Global:DiscoveredFolders = @()
[int64]$Global:FolderCount = 0
[int64]$Global:FileCount = 0

$ReadPath = $ReadNaDirPath

Function GetDirInfoRecursive {
Param([String]$PathToReadRecursive)     
Write-Host "." -ForegroundColor Cyan -NoNewLine
$GetDirInfo = Read-NaDirectory $PathToReadRecursive
Foreach ($line in $GetDirInfo){
If($line.FileType -eq "directory"){
If(($line.Name -ne ".") -and ($line.Name -ne "..") -and ($line.Name -ne ".snapshot")) {
$Global:FolderCount ++
If($RecordingOnly){
$Global:DiscoveredFolders += ($PathToReadRecursive + "/" + $line.Name)
}
If($SearchingFolders){
Foreach ($Folder in $Folders){
If($line.Name -eq $Folder){
$Global:DiscoveredFolders += ($PathToReadRecursive + "/" + $line.Name)
}
}
}
If ($line.Empty -ne "False"){
$NewPathToRead = $PathToReadRecursive + "/" + $line.Name
GetDirInfoRecursive $NewPathToRead              
}
}                   
} elseif($line.FileType -eq "file"){
$Global:FileCount ++
If($RecordingOnly){
$FileDetails = ($line.Name) + " @ " + $PathToReadRecursive
$Global:DiscoveredFiles += $FileDetails               
}
If($SearchingFiles){
Foreach ($File in $Files){
If($line.name -eq $File){
$FileDetails = ($line.Name) + " @ " + $PathToReadRecursive
$Global:DiscoveredFiles += $FileDetails
}
}
}
}
}
}

GetDirInfoRecursive $ReadPath

$FinishDate = date
"";"";">>> FINISH TIME = $FinishDate";""

###############
## 3: OUTPUT ##
###############

"<<<<< PART 3/3: OUTPUT >>>>>"
[System.Array]$Output = @()

"";">>> 3.1: DISCOVERED FOLDERS:";""
$Global:DiscoveredFolders
$Output += "","##### DISCOVERED FOLDERS #####","",$Global:DiscoveredFolders

"";">>> 3.2: DISCOVERED FILES:";""
$Global:DiscoveredFiles
$Output += "","##### DISCOVERED FILES #####","",$Global:DiscoveredFiles

"";">>> 3.3: SEARCH STATISTICS:";""
"Processed " + $Global:FileCount + " files and " + $Global:FolderCount + " folders.";""
"Started at $StartDate"
"Finished at $FinishDate";""
$Output += "","##### SEARCH STATISTCS #####","",("Processed " + $Global:FileCount + " files and " + $Global:FolderCount + " folders."),"","Started at $StartDate","Finished at $FinishDate",""

## OUTPUT TO NOTEPAD ##
## ================= ##

$UsersProfilePath = $env:USERPROFILE
$Date = Get-Date -uformat "%Y%m%d%H%M"
$OutputFile = $UsersProfilePath + "\Search-NaFileSystem_Results_" + $7Gsystem + "_" + $date + ".txt"
$Output | Out-File $OutputFile
Notepad $OutputFile

No comments:

Post a Comment