There’s one particular part of the NetApp PowerShell Toolkit which is not frequently used, but is extremely powerful. Templates can be created for many of the object types which are used to create a query for specific objects, or for limiting the amount of information returned from a particular cmdlet invocation.
To get started, we first need to initialize the object for our query or attribute limiting template. To do this we use the -Template
parameter to our cmdlet.
# create an empty Aggregate object template $aggrTemplate = Get-NcAggr -Template
If we were to look at this object it is empty:
Many of the properties associated with an object, such as an aggregate, volume, or LUN, are objects themselves. If we want to use a property of a child object as the query filter then we need to initialize that property in the template object.
# create an empty Aggregate object template $aggrTemplate = Get-NcAggr -Template # initialize a property of the template Initialize-NcObjectProperty -Object $aggrTemplate -Name AggrRaidAttributes # alternatively, initialize all properties during template creation $aggrTemplate = Get-NcAggr -Template -Full
Our template object now has a property object which has been populated:
At this point we’re ready to use the template, let’s look at how to use it as a query or to limit the attributes returned.
Query / Filter Templates
Using query templates means that filtering happens on the NetApp, not the client side. If you execute Get-NcVol
against your cluster which has thousands of volumes it may take a bit for that to execute, and when it does your client will have to work hard to process the result and turn it into objects. This is even more important when you’re looking for volumes with specific properties…instead of all volumes being returned and then piping to Where-Object
to select a handful.
# create an empty Aggregate object template $aggrTemplate = Get-NcAggr -Template # initialize a property of the template Initialize-NcObjectProperty -Object $aggrTemplate -Name AggrRaidAttributes # specify the value of the property we want to filter # in this case we only want hybrid aggregates $aggrTemplate.AggrRaidAttributes.IsHybrid = $true # execute the query Get-NcAggr -Query $aggrTemplate
At this point the returned objects would be exactly as expected, they contain all properties of a standard Get-*
cmdlet invocation.
Queries without the template
There is also a way to shortcut doing a query using hash tables. This makes it super easy to do queries on the NetApp side and eliminate work from the client.
# query for volumes on a particular aggregate Get-NcVol -Query @{Volume=$volName} # we can nest the hashes as well for those attributes which are objects # for example, get all volumes which are not node root volumes Get-NcVol -Query @{VolumeStateAttributes=@{IsNodeRoot=$false}}
Query with wildcards and relative numbers
The extremely simple examples above use static values for the queried properties, but we can use wildcards and other operators on the values as well.
# get all volumes more than 10TB in size Get-NcVol -Query @{TotalSize=">$(10TB)"} # get volumes with root in the name Get-NcVol -Query @{Name="*root*"} # get volumes without root at the end of the name Get-NcVol -Query @{Name="!*root"} # get disks which are not a part of an aggregate Get-NcDisk -Query @{DiskRaidInfo=@{ContainerType="!aggregate"}} # non-root volumes which are between 100GB and 1TB in size Get-NcVol -Query @{VolumeStateAttributes=@{IsNodeRoot=$false};TotalSize="$(100GB)..$(1TB)";}
The list of query operators we can use here is the same as at the CLI. I haven’t show them here, but OR
(|
), less than or equal to
(<=
), and greater than or equal to
(>=
) are all available as well.
Attribute Limiting Templates
For many of the objects returned by the Toolkit there is a huge amount of information available. This is useful when we don't know what information we're looking for, or if we want to explore around and see what information is in the result. But, if we are executing a script which doesn't need all that extra info it can greatly speed up execution by reducing the information returned...not to mention remove that extra work from the system on the other end.
# create an empty Aggregate object template $aggrTemplate = Get-NcAggr -Template # using this empty object will return only the bare minimum properties # for each aggregate object, which is really only the name Get-NcAggr -Attributes $aggrTemplate
Returning only the name is helpful if we only need that information, for example if we're piping into a Foreach-Object
loop and querying for volumes per aggregate. Notice that with this empty template none of the other properties in the output are populated.
If we want additional properties we use the same Initialize-NcObjectProperty
cmdlet to specify the properties.
# create an empty Aggregate object template $aggrTemplate = Get-NcAggr -Template # initialize the AggregateSpaceAttributes attribute to get only space information Initialize-NcObjectProperty -Object $aggrTemplate -Name AggrSpaceAttributes # we can now retrieve the aggregates with only the space information returned Get-NcAggr -Attributes $aggrTemplate # note that the shortcut method with hashes works here too Get-NcAggr -Attributes @{AggrSpaceAttributes=@{}}
Templates are awesome!
Templates are a powerful feature which can help you to significantly speed up the execution of your scripts. For proof, here are two examples where filtering on the storage side reduces the execution time by 75%, and returning only the properties needed reduces execution time by almost 85%!
The post NetApp PowerShell Toolkit – Templates appeared first on The Practical Administrator.