Thursday, February 4, 2010

Using PowerShell with NetApp to Remove Snapshots

Most VMware administrators have learned to use PowerShell for scripting. VMware has done a wonderful job with its PowerCLI to embrace this scripting language.

VMware is often implemented on NetApp due to some wonderful advantages with NFS, de-duplication, VDI and Snapshots. Thanks to work in a codeplex project known as PoshOnTap, it is possible to manage NetApp OnTap with the same ease.

A common problem is old snapshots that need to be discovered and deleted. It is simple with PoshOnTap to find all snapshots older than one week:

import-module PoshOnTap
connect-naserver -filer toaster -Credential (get-credential)
Get-NaVol | Get-NaSnapshot | where-object {$_.Created -lt (Get-Date).AddDays(-7)}



Want to delete those snapshots? You can append a " | remove-nasnapshot" to the above command:

Get-NaVol | Get-NaSnapshot | where-object {$_.Created -lt (Get-Date).AddDays(-7)} | remove-nasnapshot

I like to put a delay in between snapshot deletions so I tend to use code to introduce a 10 minute delay in between each deletion:

import-module PoshOnTap
connect-naserver -filer toaster -Credential (get-credential)

$snapshots=(Get-NaVol | Get-NaSnapshot | where-object {$_.Created -lt (Get-Date).AddDays(-7)})

foreach ($s in $snapshots) {remove-nasnapshot -name $s.Snapshot -volume $s.Volume; start-sleep 600}



PoshOnTap has a wealth of management functions for NetApp OnTap. The work done by the coders is exceptional and greatly appreciated!

1 comment:

  1. Cool utility. However, many of my snapshots have very long names (auto-generated). Most of them are showing up, but I get this error a lot:


    New-Object : Cannot convert argument "6", with value: "2154877964", for "NaSnapShot" to type "System.Int32": "Cannot
    convert value "2154877964" to type "System.Int32". Error: "Value was either too large or too small for an Int32.""
    At C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PoshOnTap\NaSnapshot.psm1:91 char:5
    + New-Object NetApp.SDK.NaSnapshot -ArgumentList @(
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
    + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

    ReplyDelete