question

MichaPawlicki-7509 avatar image
0 Votes"
MichaPawlicki-7509 asked MichaPawlicki-7509 commented

Delete old snapshots on Azure Files using PowerShell

Looks like there's still no automatic system to remove old snapshots.

I've been trying to use the method published in azure-fileshare-snapshot-powershell-deletion



but that doesn't quite work for me.

My code is simple:

 #set the contect using the connection string 
 $Context = New-AzStorageContext -ConnectionString "xyz"
    
 # Generate Snapshot List
 $SnapshotList = Get-AzStorageShare -Context $context | Where-Object {$_.Name -eq $ShareName -and $_.IsSnapshot -eq $true -and $_.SnapshotTime -lt ([datetime]::UtcNow.AddDays(-4))}
    
 # Delete Snapshots older than 4 days
 foreach ($Snapshot in $SnapshotList)
     {
        Remove-AzStorageShare -Share $Snapshot -Verbose -Force
     }


However, I'm getting

 Remove-AzStorageShare : Cannot bind parameter 'Share'. Cannot convert the "Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureS
 torageFileShare" value of type "Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageFileShare" to type "Microsoft.Azure.S
 torage.File.CloudFileShare".
 At line:3 char:37
 +        Remove-AzStorageShare -Share $Snapshot -Verbose -Force

Is there any way to grab snapshots in the type of CloudFileShare?

Thanks!

Warmly,
Michał

azure-files
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I am working to see why the script is not working and will get back with your shortly. To automate snapshots and deletion you can look into using Azure file share backup.


0 Votes 0 ·

1 Answer

deherman-MSFT avatar image
1 Vote"
deherman-MSFT answered MichaPawlicki-7509 commented

@MichaPawlicki-7509
Thanks for your patience while we looked into this issue. There is a change on File cmdlets from Az 4.1.0. The change is: the File Share cmdlets output changes from “CloudFileShare” to “AzureStorageFileShare”, the original output will become a child property of the new output. To resolve this you can make a simple change in your foreach loop:

  foreach ($Snapshot in $SnapshotList)
      {
         Remove-AzStorageShare -Share $Snapshot.CloudFileShare -Verbose -Force
      }

Hope this helps! Let us know if you have any further questions.



Please don’t forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.




· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

That was exactly what I was looking for!

Thank you!

0 Votes 0 ·