Microsoft Virtualization Discussions

get-nasnapshot with last dated snapshot

tsentekin
5,761 Views

hi all,

i'm writing a powershell script and i need to get the names of the last snapshots which are sent to dr site with snapmirror i'm trying to automate the disaster recovery scenario.

basic concept is finding the last snapshot name, create a volume flexclone from the snapshot and map the clone luns to a esxi server.

i cannot get the date values of the snapshots so i'm not able to create flexclones with them.

any help will be appreciated.

Thanks.

T.S.

1 ACCEPTED SOLUTION

bsti
5,761 Views

Try this:

Connect-NaController controller

$latestSnap = Get-NaSnapshot -TargetName volume | ? { $_.Name -imatch "snapshot root name" } | Sort-Object AccessTimeDT -Descending | Select-Object -first 1

Where snapshot root name is the name of the snapshots you are creating.  You want to filter out the automatically generated snapshots created by Snapmirror (assuming you are using SM).

View solution in original post

5 REPLIES 5

bsti
5,762 Views

Try this:

Connect-NaController controller

$latestSnap = Get-NaSnapshot -TargetName volume | ? { $_.Name -imatch "snapshot root name" } | Sort-Object AccessTimeDT -Descending | Select-Object -first 1

Where snapshot root name is the name of the snapshots you are creating.  You want to filter out the automatically generated snapshots created by Snapmirror (assuming you are using SM).

paleon
5,761 Views

The Get-NaSnapshot cmdlet should return snapshot objects that include the creation time of the snapshot.  This property is named "Created" and is of type System.DateTime.


Here is some sample code:

$vols = Get-NaVol | Sort-Object -Property "Name"

foreach ($vol in $vols) {

    write-host "Processing $vol"

    $snapshots = $null

    $snapshots = Get-NaSnapshot -TargetName $vol.name | Sort-Object -Property "Created"

    $vol | Add-Member -Name "Snapshots" -MemberType "NoteProperty" -Value $snapshots

}


This will get all the volumes on the connected NetApp.  The list will be sorted by name.  Then, for each volume, the list of snapshots is gathered and sorted by the "Created" property.  The list of snapshots is then added as a new member property of the volume object.

Now, the $vols object contains an array of volumes (or a single volume if only one volume exists).  Each volume object contains a property named "Snapshots."  If the volume contained no snapshots (or if the Get-NaSnapshot commandlet encountered an error), the Snapshots property will be $null.  If the volume contained snapshots, the array of snapshots (or a single snapshot object if only one existed) will be stored in that property.


If you want the last snapshot object for a volume, the command would look like this.


$volname = <name_of_volume_from_which_you_want_to_make_the_clone>
$volCloneParent = $vols | Where-Object {$_.name -eq $volname"}

$snapshots = $volCloneParent.Snapshots

$newestSnapshot = $snapshots[$snapshots.count-1]
$oldestSnapshot = $snapshots[0]

Please let me know whether this answers your question.


Bill

drdabbles
5,761 Views

Don't sort by the "Created" property. It does not sort the column as dates, but rather as strings. So, you'll see something like this:

1/13/2012

1/9/2012

1/12/2012

Instead, sort by the "AccessTime" property. This property does not seem to change when you create a flexclone from the snapshot, so I'm not sure why it's named "AccessTime". But either way, it's the number of seconds since Epoch, so it's pretty easy to sort by.

bsti
5,761 Views

One of the recent versions of the PoSH Toolkit also reports the AccessTimeDT property, which is AccessTime translated to a GMT datetime. 

tsentekin
5,761 Views

Thanks to everyone,especially to bsti@plex.com now i think i can complete my script. cheers everyone:))

T.S.

Public