ONTAP Discussions

How to combine Get-NcSnapshot & Get-NcAggr in a Powershell script

Ontapforrum
7,769 Views

Hi,

 

I am trying to create a simple powershell script to generate snaps older than x days, that bit is easy.

 

However, Get-NcSnapshot does not yeild 'Aggr' Name, so I have added :

Get-NcAggr | where { $_.Volume -eq $x.Volume }

 

Objective: Getting snapshots older than x days is a bit and it gives information about which volume and vserver it belongs to along with the size of the snap, but it does not tell you which aggregate the snapshot-volume belongs to. Especially in a thin environment, where you can give back space to aggr by removing the snapshot.

 

Finally outputing it to CSV, problem is : Both Get-NcSnapshot & Get-NaAggr has common 'Name' column, hence it ignores the Aggr name, and simply populates the Agg 'Name' column with 'volumename'.

 

 

Is there a way to combine both these commands despite having same column name 'Name'?

 

Thanks,
-Ashwin

1 ACCEPTED SOLUTION

donny_lang
7,609 Views

Yeah, the "ForEach-Object" in your code is causing that particular behavior. I reworked it a bit, try this:

 

Get-NcSnapshot | Where-Object { $_.Created -lt (Get-Date).AddDays($value) } | Select-Object @{l = "SnapName"; e = {$_.Name}}, Created, Vserver, Volume, Dependency, `
	@{l = "Cumulative_GB"; e = {$([Math]::Round($_.Cumulative / 1GB, 2)).ToString()}}, @{l = "Total_GB"; e = {$([Math]::Round($_.Total / 1GB, 2)).ToString() }}, `
	@{l = "Aggregate"; e = { (Get-NcVol $_.Volume).Aggregate } } | Export-CSV -Path "\destination\path\snaps.csv"

This returned good data in my lab, so hopefully it works for you too. 🙂

View solution in original post

8 REPLIES 8

GidonMarcus
7,687 Views

you can add the data to a new column using add-member

$snaps = get-ncsnapsot | %{$_ | Add-Member -type NoteProperty -name AggrName -Value ($_ | get-ncvol).Aggregate}

 * i didn't tested it as i don't have the simulator running now...

Gidi

Gidi Marcus (Linkedin) - Storage and Microsoft technologies consultant - Hydro IT LTD - UK

donny_lang
7,662 Views

@GidonMarcus I get an "get-ncvol : Unable to find API: volume-get-iter on node vserver lab-clst-01-n1" error when attempting to run your code (ONTAP 9.6 and PS Toolkit 9.6).

 

You can use Select-Object to add a calculated property to your output though like this (will get all snapshots older than 90 days)

$90days = (get-date).adddays(-90)
Get-NcSnapshot | Where-Object { ($_.Created -lt "$90days") } | Select-Object Name, Vserver, Created, Total, @{l="Aggregate";e={(Get-NcVol $_.Volume).Aggregate}} | Format-Table

Donny

 

 

Ontapforrum
7,621 Views

Hi Donny & Gidi,

 

Many thanks for chipping  in and helping out. Much appreciated.

 

@Donny: I used the command you suggested in the following script: It will be interactive, i.e it will ask user to input the 'days older than snaps'  in integer and depending on that it will produce result.

 

Its working fine without aggr field, my intention is to get aggregte against the volume names.


I tried this, and it runs but never ends [goes in loop kinds] I had to kill it, but it produced the resutls in excel, however the aggregate colume puts all the aggregates in each cell instead of just one aggregate against a column name.

 

Any idea how can I get this fixed ?

 

 

Get-NcSnapshot | ForEach-Object {
$x = "" | Select @{Name="SnapName";Expression={$_.Name}},Created, Vserver, Volume, Total_GB, Cumulative_GB, Dependency, @{l="Aggregate";e={(Get-NcVol $_.Volume).Aggregate}}
$x.SnapName = $_.Name
$x.Created = $_.Created
$x.Vserver = $_.Vserver
$x.Volume = $_.Volume
$x.Total_GB = $_.Total
$x.Cumulative_GB = $_.Cumulative
$x.Dependency = $_.Dependency
$x.Total_GB = [Math]::Round($_.Total / 1GB, 2)
$x.Cumulative_GB = [Math]::Round($_.Cumulative / 1GB, 2)
$x
} | Where {$_.Created -lt (Get-Date).AddDays(-$value)} | Export-CSV -Path "C:\blah\blah\snaps.csv"

Ontapforrum
7,612 Views

Hi Guys,

 

I think  I understood the issue but don't know how to fix it. I am sure there is simple fix for this.

 

Whats happening is : Get-NcVol is  looking at $_.Volume  and therefore it is going through all the volumes that is paased by Get-NcSnapshot and then getting all the aggreagtes and inserting entire list in single cell against each snapshot. Instead of just taking one volume at a time for the respective snapshot and then getting aggregate name for that one volume alone and then next one and so on.

donny_lang
7,610 Views

Yeah, the "ForEach-Object" in your code is causing that particular behavior. I reworked it a bit, try this:

 

Get-NcSnapshot | Where-Object { $_.Created -lt (Get-Date).AddDays($value) } | Select-Object @{l = "SnapName"; e = {$_.Name}}, Created, Vserver, Volume, Dependency, `
	@{l = "Cumulative_GB"; e = {$([Math]::Round($_.Cumulative / 1GB, 2)).ToString()}}, @{l = "Total_GB"; e = {$([Math]::Round($_.Total / 1GB, 2)).ToString() }}, `
	@{l = "Aggregate"; e = { (Get-NcVol $_.Volume).Aggregate } } | Export-CSV -Path "\destination\path\snaps.csv"

This returned good data in my lab, so hopefully it works for you too. 🙂

Ontapforrum
7,589 Views

Hi Donny,

 

You are absolutely star. Didn't have to edit anything, just copy pasted and it worked like a charm.

 

Wow..that's a beauty of community support, I don't have to break my head for something for which there are experts available already.

 

Just being a little more greedy, just a query : I can easily take this aggr name and check it's used %  But, I was wondering if I can feed in - Get-NcAggr and pull in $_.Aggregate from the Get-NcVol ouput and calcualte the % used, total , availabe in the same excel. I want to know how to insert another Get-NcAggr in that script.

 

Many thanks again.

 

You rock.

 

Thanks,

-Ash

Ontapforrum
7,580 Views

******Sorry I accidently clicked the wrong reply message for Accepted solution ***** Can Moderator please undo this ?

AlexDawson
7,420 Views

Done, thanks for letting us know and thanks @donny_lang for helping out!

Public