Using Custom PS Objects to Quickly Filter Data from Custom PS Modules

Many times when working with Powershell Modules, I’m presented with an object that contains data that I want, but it’s embedded inside nested hash tables, or isn’t quite storing the data in a way that’s useful for me to work with.

For example, let’s take the output of a function from Rubrik’s Powershell Module – “Get-RubrikSLA“. This command will output all the information about various backup policies, but doesn’t quite present it in a usable format for me (Say to output to a CSV file).

id : 1d3e635d-ad98-438a-9738-a0098a3ff987
primaryClusterId : 98003aa6-c7e9-4410-a79e-3d5c72f3109e
name : Daily-Weekly
frequencies : @{daily=; weekly=}
allowedBackupWindows : {}
firstFullAllowedBackupWindows : {}
maxLocalRetentionLimit : 2419200
archivalSpecs : {}
replicationSpecs : {}
numDbs : 0
numOracleDbs : 0
numFilesets : 0
numHypervVms : 0
numNutanixVms : 0
numManagedVolumes : 0
numStorageArrayVolumeGroups : 0
numWindowsVolumeGroups : 0
numLinuxHosts : 0
numShares : 0
numWindowsHosts : 0
numVms : 180
numEc2Instances : 0
numVcdVapps : 0
numProtectedObjects : 180
isDefault : False
uiColor : #ae00ff
showAdvancedUi : True
advancedUiConfig : {@{timeUnit=Daily; retentionType=Daily}, @{timeUnit=Weekly; retentionType=Weekly}}

id : 4670304c-3011-465b-9bbf-32c8e8b45ea0
primaryClusterId : 98003aa6-c7e9-4410-a79e-3d5c72f3109e
name : Daily-Weekly-SQL
frequencies : @{daily=; weekly=; monthly=; yearly=}
allowedBackupWindows : {}
firstFullAllowedBackupWindows : {}
maxLocalRetentionLimit : 63072000
archivalSpecs : {}
replicationSpecs : {}
numDbs : 157
numOracleDbs : 0
numFilesets : 0
numHypervVms : 0
numNutanixVms : 0
numManagedVolumes : 0
numStorageArrayVolumeGroups : 0
numWindowsVolumeGroups : 0
numLinuxHosts : 0
numShares : 0
numWindowsHosts : 0
numVms : 0
numEc2Instances : 0
numVcdVapps : 0
numProtectedObjects : 157
isDefault : False
uiColor : #ff574
showAdvancedUi : True
advancedUiConfig : {@{timeUnit=Daily; retentionType=Daily}, @{timeUnit=Weekly; retentionType=Weekly}, @{timeUnit=Monthly; retentionType=Monthly}, @{timeUnit=Yearly;
retentionType=Yearly}}

id : 4722fced-b920-49e3-975c-5cbc19927022
primaryClusterId : 98003aa6-c7e9-4410-a79e-3d5c72f3109e
name : Remote-FS
frequencies : @{daily=; weekly=; monthly=}
allowedBackupWindows : {@{startTimeAttributes=; durationInHours=12}}
firstFullAllowedBackupWindows : {}
localRetentionLimit : 63072000
maxLocalRetentionLimit : 62208000
archivalSpecs : {@{locationId=e7cd50b6-8ed1-4067-bf7e-93c51627b043; archivalThreshold=1}}
replicationSpecs : {}
numDbs : 0
numOracleDbs : 0
numFilesets : 47
numHypervVms : 0
numNutanixVms : 0
numManagedVolumes : 0
numStorageArrayVolumeGroups : 0
numWindowsVolumeGroups : 0
numLinuxHosts : 0
numShares : 0
numWindowsHosts : 45
numVms : 0
numEc2Instances : 0
numVcdVapps : 0
numProtectedObjects : 45
isDefault : False
uiColor : #256d2d
showAdvancedUi : True
advancedUiConfig : {@{timeUnit=Daily; retentionType=Daily}, @{timeUnit=Weekly; retentionType=Weekly}, @{timeUnit=Monthly; retentionType=Monthly}}

I’m interested in the frequencies of the backups, the name of the SLA Domain, and not much else. If we try to export the data right now using Select-Object we will see that the frequencies are inside an embedded hash table. When running “Export-CSV”, we’ll just be presented with this in our row –

frequencies : @{daily=; weekly=; monthly=}

Really, the data we are interested in is inside another has table called Daily. Drilling down, we can see that 'Daily' contains a the key;value pair Frequency,Retetention

PS /Users/nshores/Documents/PCCI> $s.frequencies.daily                                                                                                                                          

frequency retention
--------- ---------
        1        10

The way to work around this limitation is to create a script that utilizes custom PS objects. This script will loop over the object we examined above, and push the data we want from each nested hash table, into a custom object called $slaOBjRecord . This object will then be added to an array, $sla_report_array.

Using this technique we end up with a result that looks like the table below, and can be used with export-csv without any issue.

PS /Users/nshores/Documents/PCCI> $sla_report_array | ft                                                                                                                                        

Name             Weekly Freq Monthly Freq Objects Weekly Retention Monthly Retention Daily Retention Daily Freq
----             ----------- ------------ ------- ---------------- ----------------- --------------- ----------
Daily-Weekly               1                  180                4                                10          1
Daily-Weekly-SQL           1 1                157                4 3                              10          1
Remote-FS                  1 1                 45                4 24                             10          1
Daily-Monthly              1 1                 48                4 3                              10          1
Daily-SAP                  1 1                  1                4 3                               7          1
Test-VMs                   1 1                 25                4 3                              10          1
Share this content:

Leave a Comment

Your email address will not be published. Required fields are marked *