Search-NcFileSystem.ps1 (cDOT)


Note: Formatted for blogger (tabs have been removed). Copy and paste into a text file and save as Search-NcFileSystem.ps1.

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

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

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

"";"+++++ Search-NcFileSystem.ps1 (cDOT) +++++";""

#############################################
## 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 >>>>>";""

## CLUSTER/USERNAME/PASSWORD/VSERVER ##
## ================================= ##

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

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

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

$GetNcVserver = Get-NcVserver $Vserver
If(!$GetNcVserver){"Unable to connect to $Vserver - exiting!"; ""; EXIT}
$Global:CurrentNcController.Vserver = $Vserver

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

">>> List of Volumes in $Vserver "; ""
$Attributes = Get-NcVol -Template
$Query      = Get-NcVol -Template
Initialize-NcObjectProperty -object $Query -name VolumeStateAttributes
$Query.VolumeStateAttributes.IsVserverRoot = ""
[System.Array]$VolumesList = (Get-NcVol -Attributes $Attributes -Query $Query).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-NcDirectory uses /vol/VOLNAME = /vol/$Volume"
$ReadNcDirPath = "/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

$Attributes           = Read-NcDirectory -Template
$Attributes.Empty     = ""
$Attributes.FileType  = ""
$Attributes.Name      = ""

$ReadPath = $ReadNcDirPath

Function GetDirInfoRecursive {
Param([String]$PathToReadRecursive)     
Write-Host "." -ForegroundColor Cyan -NoNewLine
$GetDirInfo = Read-NcDirectory $PathToReadRecursive -Attributes $Attributes
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-NcFileSystem_Results_" + $Cluster + "_" + $Vserver + "_" + $date + ".txt"
$Output | Out-File $OutputFile
Notepad $OutputFile

Comments