Saturday, 18 February 2017

Treesize for Data ONTAP 7-Mode (Get-NaTreesize.ps1)

Continuing from the previous post...

Since it is pretty trivial to convert Get-NcTreesize.ps1 into a Data ONTAP 7-Mode version, here it is.

A usage example>


$P = Read-Host -AsSecureString

.\Get-NaTreeSize.ps1 -Controller 10.1.7.50 -UserName root -Password $P -ScanStartPath /vol/TVOL001 -OutputFile "7M_TVOL001.CSV"


Image: Example of the raw, unformatted CSV output
The Script


####################
## Get-NaTreesize ##
####################

## A few differences in the 7-Mode version:
# 1) Don't need to specify vFilers!
# 2) There is no -Template with *na* cmdlets.
# 3) There is no Get-NaFileDirectorySecurity.
# N.B.: The APIs in 7-Mode are noticeably slower than in C-Mode!

Param(
  [Parameter(Mandatory=$True)][String]$Controller,
  [Parameter(Mandatory=$True)][String]$UserName,
  [Parameter(Mandatory=$True)][Security.SecureString]$Password,
  # TIP: PS> $P = Read-Host -AsSecureString
  # ... you can use $P after -Password
  [Parameter(Mandatory=$True,HelpMessage="/vol/VOLNAME(/...)")]
  [String]$ScanStartPath,
  [String]$OutputFile = "NaTreesizeReport.CSV",
  [String]$DateFormat = "yyyy.MM.dd"
);""

## GENERIC: Loading PSTK ##
"INFO: DataONTAP PSTK loading ..."
[Void](Import-Module DataONTAP)
If(!(Get-Module DataONTAP)){"ERROR: No DataONTAP PSTK!";EXIT}
"INFO: DataONTAP PSTK loaded.`n"

## GENERIC: Connecting to $Controller ##
"INFO: $Controller connecting ..."
$Global:CurrentNaController = $NULL
$Cred = New-Object System.Management.Automation.PsCredential($UserName,$Password)
[Void](Connect-NaController -Name $Controller -Credential $Cred)
If(!$Global:CurrentNaController){"ERROR: Cannot connect to $Controller!";EXIT}
"INFO: $Controller connected.`n"

## VERIFY $ScanStartPath ##
"INFO: $ScanStartPath verifying ..."
If(!(Read-NaDirectory $ScanStartPath)){"ERROR: $ScanStartPath is incorrect!";EXIT}
"INFO: $ScanStartPath verified.`n"

## GLOBAL VARIABLES ##
[System.Array]$Global:CSV = @() # Output CSV
[Int64]$Global:Order = 0 # For output ordering

## RECURSIVE SCANNING FUNCTION ##
Function GetDirInfoRecursive{
  Param([String]$Path)
  Write-Host "." -NoNewLine # Indicates the program is running.
  $GetDirInfo = Read-NaDirectory $Path
  If(!$GetDirInfo){Return 0} # Return 0 size if failed to read $Path
  $Global:Order ++
  $Local:Order   = $Global:Order
  $Local:DirSize = 0
  [Int]$level    = $Path.Split("/").count - 2
 
  ## CYCLE THROUGH $GetDirInfo ##
  Foreach ($l in $GetDirInfo){          
    If($l.FileType -eq "directory"){
      If($l.Name -eq "."){$CurrentDir = $l}
      If( (".","..",".snapshot" -notcontains $l.Name) -and ($l.IsEmpty -ne "False") ){
        $Local:DirSize += (GetDirInfoRecursive ($Path + "/" + $l.Name))
      }
    }elseif($l.FileType -eq "file"){           
      $Global:Order ++
      $Local:DirSize += $l.FileSize
     
      ## ADD FILE INFORMATION TO CSV ##
      $Global:CSV += [PSCustomObject]@{
        "Order"           = $Global:Order
        "Directory Level" = $Level
        "File Type"       = "file"
        "Name"            = $l.Name
        "Path"            = $Path
        "File Size"       = $l.FileSize
        "Directory Size"  = ""
        "AclType"         = $l.AclType
        "OwnerId"         = $l.OwnerId
        "GroupId"         = $l.GroupId
        "Perm"            = $l.Perm
        "Accessed Time"   = $l.AccessedTimeStampDT.ToString($DateFormat)
        "Changed Time"    = $l.ChangedTimeStampDT.ToString($DateFormat)
        "Creation Time"   = $l.CreationTimeStampDT.ToString($DateFormat)
      }
    }
  }
  ## ADD DIRECTORY INFORMATION TO CSV ##
  $Global:CSV += [PSCustomObject]@{
    "Order"           = $Local:Order
    "Directory Level" = $Level
    "File Type"       = "directory"
    "Name"            = $Path.Split("/")[$level + 1]
    "Path"            = $Path
    "File Size"       = ""
    "Directory Size"  = $Local:DirSize
    "AclType"         = $CurrentDir.AclType
    "OwnerId"         = $CurrentDir.OwnerId
    "GroupId"         = $CurrentDir.GroupId
    "Perm"            = $CurrentDir.Perm       
    "Accessed Time"   = $CurrentDir.AccessedTimeStampDT.ToString($DateFormat)
    "Changed Time"    = $CurrentDir.ChangedTimeStampDT.ToString($DateFormat)
    "Creation Time"   = $CurrentDir.CreationTimeStampDT.ToString($DateFormat)
  }
  Return $Local:DirSize
}

##################
## MAIN PROGRAM ##
##################

("START: " + [String](Get-Date))
[Void](GetDirInfoRecursive $ScanStartPath);""
("END: " + [String](Get-Date) + "`n")
$Global:CSV | Sort "Order" | Export-CSV "$OutputFile" -NoTypeInformation


No comments:

Post a Comment