none
Azure FileShare SnapShot Powershell Deletion RRS feed

  • Question

  • Having a hard time finding the best way to delete a Azure FileShare Snapshot from powershell. It appears the way to delete that I got to work, is referencing the snapshot you just took. Has anyone been able to remove file share snapshots easily in powershell??

     

    Example here I have a small script that gathers the snapshots that are 4 days or older

     

    $keyvaultsecret = (Get-AzKeyVaultSecret -vaultName "keyvault" -name "secretname").SecretValueText

    $context = New-AzStorageContext -StorageAccountName "storageaccountname" -StorageAccountKey $keyvaultsecret

    $snapshotlist = Get-AzStorageShare -Context $context | Where-Object { $_.Name -eq "sharename" -and $_.IsSnapshot -eq $true -and $_.SnapshotTime -lt ([datetime]::UtcNow.AddDays(-4))}

     

    Now I am unsure on how to incorporate the Remove-AzStorageShare here to delete the list it generates…. It appears here is the only example from Microsoft I was able to find https://docs.microsoft.com/en-us/azure/storage/files/storage-how-to-use-files-powershell

     

    Where I would have to go and the below works to delete the current snapshot

    #Create Snapshot

    $keyvaultsecret = (Get-AzKeyVaultSecret -vaultName "keyvault" -name "secretname").SecretValueText

    $context = New-AzStorageContext -StorageAccountName "storageaccountname" -StorageAccountKey $keyvaultsecret

    $share = Get-AzStorageShare -Context $context -Name "sharename"

    $snapshot = $share.Snapshot()

     

    #Delete Snapshot

    Remove-AzStorageShare -Share $snapshot

     

    Is there a good way to use the Remove-AzStorageShare command to delete a custom list of snapshots easily? If anyone has any examples that would be awesome….

     

     

    Thanks!

    Monday, August 19, 2019 11:58 PM

Answers

  • FYI.... I came up with a fix as Will Gries kind of gave me the idea to set me on the proper path but it's different... I do appreciate the comments of Will as well... thank you for helping

    I use it as an automation script to take snapshots every hour and then clear anything older than 4 days as the max is 200 Snapshots. Set your parameters and let it go.... I still was setting storage context via vault as that is what I wanted, but of course anyone could change as they needed for that.

    Param(
     [string]$KeyVaultName,
     [string]$SecretName,
     [string]$StorageAccountName,
     [string]$FileShareName
    )

    # Authenticate with your Automation Account

    $Conn = Get-AutomationConnection -Name AzureRunAsConnection
    Add-AzAccount -ServicePrincipal -Tenant $Conn.TenantID `
    -ApplicationID $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint

    #Create Snapshot
    $keyvaultsecret = (Get-AzKeyVaultSecret -vaultName $KeyVaultName -name $SecretName).SecretValueText
    $context = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $keyvaultsecret
    $share = Get-AzStorageShare -Context $context -Name $FileShareName
    $share.Snapshot()


    # 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
        }

    SO if anyone ever needed to know something like taking snapshots of azure fileshare and cleaning it up, this would be a way of doing it.


    • Marked as answer by LukeUhren Tuesday, August 20, 2019 7:46 PM
    • Edited by LukeUhren Wednesday, August 21, 2019 2:39 AM
    Tuesday, August 20, 2019 7:45 PM

All replies

  • You actually don't need to generate a storage account context with the New-AzStorageContext (although there's nothing wrong with it). You can do this too:

    $storAcct = Get-AzStorageAccount -ResourceGroupName "myrg" -Name "mystoracct"
    Get-AzStorageShare -Context $storAcct.Context

    If you have a list of snapshots that you've generated somehow, you can use the ForEach-Object cmdlet to run the Remove-AzStorageShare cmdlet in a loop:

    $customSnapshots = "8/19/2019 9:25:38 PM +00:00", "8/19/2019 9:25:54 PM +00:00"
    $customSnapshot | ForEach-Object { Remove-AzStorageShare -Share (Get-AzStorageShare -Context $storAcct.Context -Name "myshare" -SnapshotTime $_) }
    

    Hope this helps!

    Thanks,

    Will Gries
    Senior Program Manager, Azure Files


    Tuesday, August 20, 2019 4:46 AM
  • Just checking in to see if the above answer helped. If this answers your query, do click “Mark as Answer” and Up-Vote for the same, which might be beneficial to other community members reading this thread. And, if you have any further query do let us know.
    Tuesday, August 20, 2019 11:45 AM
    Moderator
  • That does help more for sure, but I am still not there. I will let you know what I come up with though when I get it working.

    Thanks!

    Tuesday, August 20, 2019 3:19 PM
  • FYI.... I came up with a fix as Will Gries kind of gave me the idea to set me on the proper path but it's different... I do appreciate the comments of Will as well... thank you for helping

    I use it as an automation script to take snapshots every hour and then clear anything older than 4 days as the max is 200 Snapshots. Set your parameters and let it go.... I still was setting storage context via vault as that is what I wanted, but of course anyone could change as they needed for that.

    Param(
     [string]$KeyVaultName,
     [string]$SecretName,
     [string]$StorageAccountName,
     [string]$FileShareName
    )

    # Authenticate with your Automation Account

    $Conn = Get-AutomationConnection -Name AzureRunAsConnection
    Add-AzAccount -ServicePrincipal -Tenant $Conn.TenantID `
    -ApplicationID $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint

    #Create Snapshot
    $keyvaultsecret = (Get-AzKeyVaultSecret -vaultName $KeyVaultName -name $SecretName).SecretValueText
    $context = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $keyvaultsecret
    $share = Get-AzStorageShare -Context $context -Name $FileShareName
    $share.Snapshot()


    # 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
        }

    SO if anyone ever needed to know something like taking snapshots of azure fileshare and cleaning it up, this would be a way of doing it.


    • Marked as answer by LukeUhren Tuesday, August 20, 2019 7:46 PM
    • Edited by LukeUhren Wednesday, August 21, 2019 2:39 AM
    Tuesday, August 20, 2019 7:45 PM
  • @LukeUhren Glad to know that the issue is resolved. Thanks for the update! Kindly do let us know if you need further assistance.
    Wednesday, August 21, 2019 6:37 AM
    Moderator