Microsoft Virtualization Discussions

Powershell - Get a list of volumes that have not created a snapshot in the last # of hours

Tmbridges
5,152 Views

I am trying to create a powershell script that will give me every volume that has not created a snaphot is the last 2 hours.

 get-ncvol | get-ncsnapshot | where-object {$_.createdhour -g 2}

 

this only give me snapshots creat at 2 am 

1 ACCEPTED SOLUTION

donny_lang
5,074 Views

Try this code:

$vols = Get-NcVol
foreach ($vol in $vols) {
	Get-NcSnapshot | Where-Object { $_.Created -le (Get-Date).AddHours(-2) -and $_.Volume -eq $vol } | Sort-Object AccessTimeDT -Descending | Select-Object -First 1 `
	@{l = "Volume"; e = { $_.Volume } }, @{l = "Snapshot"; e = { $_.Name } }, @{l = "CreatedTime"; e = {$_.Created}}
}

 

I used some calculated properties to clean up the output a bit, but you can customize it too if there are additional properties you want to show beyond what I have there.

 

Hope that helps!

 

Donny

 

View solution in original post

3 REPLIES 3

Ontapforrum
5,131 Views

I am not an ps expert, but there is way to get to know the same.

 

Following simple CLI from clustershell, will return the list of volumes thta have snapshot created in less than 2 hours.

::> volume snapshot show -create-time >2h


Basically, if 'no-ouput' that means all the volumes here are candidate, if you do get 'some-output' that means except those rest all are the candidate.


Hopefully, some ps expert could provide you with ps commands you need.

 

I know this is not efficient but that's all I can think of now.

donny_lang
5,075 Views

Try this code:

$vols = Get-NcVol
foreach ($vol in $vols) {
	Get-NcSnapshot | Where-Object { $_.Created -le (Get-Date).AddHours(-2) -and $_.Volume -eq $vol } | Sort-Object AccessTimeDT -Descending | Select-Object -First 1 `
	@{l = "Volume"; e = { $_.Volume } }, @{l = "Snapshot"; e = { $_.Name } }, @{l = "CreatedTime"; e = {$_.Created}}
}

 

I used some calculated properties to clean up the output a bit, but you can customize it too if there are additional properties you want to show beyond what I have there.

 

Hope that helps!

 

Donny

 

Tmbridges
4,952 Views

Thanks!

Public