Sunday, 17 August 2014

Recursively Display All Member and Sub-Member Data from a PowerShell Get Command’s Output

Introduction

This is something rather nice. It was designed for displaying all the data pulled via DataONTAP PowerShell toolkit Get-Nc type cmdlets, but no reason why it wouldn’t work with just about every other kind of PowerShell Get output.

It’s basically a set of functions that you can either add to your scripts, or save as say “Display-All.ps1” and then load into PowerShell using “. .\Display-All.ps1” and then simply run with the Display-All command. It will take a text string as input or a variable as input.

The main function is Display-All; this calls the function Display-AllMembers, which then calls the function MemberData. This is a recursive function since MemberData will call back to Display-AllMembers when it comes across members that have sub-members. It fully maps out all the members and sub-members.

User Guide

It’s easiest to demonstrate using an example:

1) Either add these functions to your custom PowerShell modules/scripts or save as say “Display-All.ps1” and invoke into PowerShell with:

. .\Display-All.ps1

2) See what the standard Get output is with:

Get-NcAggr

3) See the enhanced output with:

Display-All Get-NcAggr

4) It would also work if you did:

$getNcAggr = Get-NcAggr
Display-All $getNcAggr

5) If you have arguments to add to the Get command, contain them in quotation marks:

Display-All “Get-NcAggr AGGREGATENAME”

6) There are also the following switches you can add:

-all

… will show the members that have no member data (i.e. members with data = $null)

-PadR {INTEGER}

… controls who far to the right the = sign is placed at (Default = 30.)

Note: Example output is in the Appendix after the script.

The Script

#############################################
## Function: Display-All and sub functions ##
#############################################

Function Display-All {

Param($getCmd,[int]$padR=30,[switch]$all=$null)
If(!$getCmd){return}
If($getCmd.gettype().name -eq "String"){$parsedCmd = Invoke-Expression $getCmd}else{$parsedCmd = $getCmd}
If(!$parsedCmd){return}
If($parsedCmd.count -eq 1){Display-AllMembers $parsedCmd;""}else{$parsedCmd | foreach{Display-AllMembers $_;""}}}

Function Display-AllMembers{

Param($element,[int]$padL=1)
($element|gm) | foreach{MemberData $element $_}}
      
Function MemberData {

Param($element,$member)
If($member.MemberType -eq "Method"){return}
$name    = $member.Name
$checks  = "Specified","NcController"
foreach($check in $checks){If($name.Contains($check)){return}}
$data    = $element.$name
$padding = $name.length + $padL
$output  = $name.PadLeft($padding,".").PadRight($padR," ").SubString(0,$padR) + " = " + $data
If (!$data){If($all){return $output}else{return}}
$checks  = "bool","string[]","NcController","System.Object"
foreach($check in $checks){If($member.Definition.Contains($check)){return $output}}
$checks  = "String","Boolean","TimeSpan","DateTime"
foreach($check in $checks){If($data.gettype().name -eq $check){return $output}}
If (($data|gm).count -eq 0){return $output}
$name.PadLeft($padding,".");$padL++
$output  = Display-AllMembers $data $padL
return $output}

Appendix: Example Output

PS C:\> Display-All get-ncaggr
.Aggr64bitUpgradeAttributes
..AggrStatusAttributes
.AggregateName                 = aggr0
.AggregateUuid                 = d83bce04-921a-49b3-8757-4d89d682aa34
.AggrFsAttributes
..BlockType                    = 64_bit
..Fsid                         = 1926854321
..Type                         = aggr
.AggrInodeAttributes
..FilesPrivateUsed             = 512
..FilesTotal                   = 31142
..FilesUsed                    = 96
..InodefilePrivateCapacity     = 30384
..InodefilePublicCapacity      = 30384
..MaxfilesAvailable            = 31142
..MaxfilesPossible             = 729592
..MaxfilesUsed                 = 96
.AggrOwnershipAttributes
..HomeId                       = 4082368507
..HomeName                     = NACLU1-01
..OwnerId                      = 4082368507
..OwnerName                    = NACLU1-01
.AggrPerformanceAttributes
..FreeSpaceRealloc             = off
.AggrRaidAttributes
..ChecksumStatus               = active
..ChecksumStyle                = block
..DiskCount                    = 5
..HaPolicy                     = cfo
..HasLocalRoot                 = True
..IsChecksumEnabled            = True
..MirrorStatus                 = unmirrored
..MountState                   = online
..PlexCount                    = 1
..Plexes
...IsOnline                    = True
...PlexName                    = /aggr0/plex0
...PlexStatus                  = normal,active
...Raidgroups
....ChecksumStyle              = block
....RaidgroupName              = /aggr0/plex0/rg0
..RaidLostWriteState           = on
..RaidSize                     = 16
..RaidStatus                   = raid_dp, normal
..RaidType                     = raid_dp
..State                        = online
.AggrSnaplockAttributes
.AggrSnapshotAttributes
..IsSnapshotAutoCreateEnabled  = True
..IsSnapshotAutoDeleteEnabled  = True
.AggrSpaceAttributes
..AggregateMetadata            = 409600
..PercentUsedCapacity          = 32
..SizeAvailable                = 1932943360
..SizeTotal                    = 2831155200
..SizeUsed                     = 898211840
..UsedIncludingSnapshotReserve = 898211840
..VolumeFootprints             = 897802240
.AggrVolumeCountAttributes
..FlexvolCount                 = 1
.Name                          = aggr0
.Nodes                         = NACLU1-01
.StripingType                  = unknown
.Available                     = 1932943360
.Disks                         = 5
.RaidSize                      = 16
.RaidType                      = raid_dp, normal
.State                         = online
.TotalSize                     = 2831155200
.Used                          = 32
.Volumes                       = 1

.Aggr64bitUpgradeAttributes
..AggrStatusAttributes
.AggregateName                 = aggr1
.AggregateUuid                 = c1d1c0b1-f38e-41a0-aacd-cd44c1974c92
.AggrFsAttributes
..BlockType                    = 64_bit
..Fsid                         = 2095167531
..Type                         = aggr
.AggrInodeAttributes
..FilesPrivateUsed             = 560
..FilesTotal                   = 31142
..FilesUsed                    = 96
..InodefilePrivateCapacity     = 31142
..InodefilePublicCapacity      = 31142
..MaxfilesAvailable            = 31142
..MaxfilesPossible             = 4863990
..MaxfilesUsed                 = 96
.AggrOwnershipAttributes
..HomeId                       = 4082368507
..HomeName                     = NACLU1-01
..OwnerId                      = 4082368507
..OwnerName                    = NACLU1-01
.AggrPerformanceAttributes
..FreeSpaceRealloc             = off
.AggrRaidAttributes
..ChecksumStatus               = active
..ChecksumStyle                = block
..DiskCount                    = 22
..HaPolicy                     = sfo
..IsChecksumEnabled            = True
..MirrorStatus                 = unmirrored
..MountState                   = online
..PlexCount                    = 1
..Plexes
...IsOnline                    = True
...PlexName                    = /aggr1/plex0
...PlexStatus                  = normal,active
...Raidgroups
....ChecksumStyle              = block
....RaidgroupName              = /aggr1/plex0/rg0
..RaidLostWriteState           = on
..RaidSize                     = 22
..RaidStatus                   = raid_dp, normal
..RaidType                     = raid_dp
..State                        = online
.AggrSnaplockAttributes
.AggrSnapmirrorAttributes
..DpSnapmirrorDestinations     = 1
.AggrSnapshotAttributes
..IsSnapshotAutoCreateEnabled  = True
..IsSnapshotAutoDeleteEnabled  = True
.AggrSpaceAttributes
..AggregateMetadata            = 897024
..PercentUsedCapacity          = 17
..SizeAvailable                = 15591985152
..SizeTotal                    = 18874368000
..SizeUsed                     = 3282382848
..UsedIncludingSnapshotReserve = 3282382848
..VolumeFootprints             = 3281485824
.AggrVolumeCountAttributes
..FlexvolCount                 = 4
.Name                          = aggr1
.Nodes                         = NACLU1-01
.StripingType                  = not_striped
.Available                     = 15591985152
.Disks                         = 22
.RaidSize                      = 22
.RaidType                      = raid_dp, normal
.State                         = online
.TotalSize                     = 18874368000
.Used                          = 17
.Volumes                       = 4


No comments:

Post a Comment