|
| 1 | +--- |
| 2 | +title: PowerShell Script - Undelete a deleted File share |
| 3 | +description: Learn how to use an Azure PowerShell script to undelete an accidentally deleted File share. |
| 4 | +ms.topic: sample |
| 5 | +ms.date: 02/02/2020 |
| 6 | +--- |
| 7 | + |
| 8 | +# PowerShell script to undelete an accidentally deleted File share |
| 9 | + |
| 10 | +This script helps you to undelete a file share, if you deleted it accidentally. The soft delete security feature for file shares provides you the option of undeleting a file share within the 14 days retention period, allowing recovery of all your file share contents, snapshots, and recovery points. To learn more about soft delete, visit this [link](../soft-delete-azure-file-share.md). |
| 11 | + |
| 12 | +## Sample script |
| 13 | + |
| 14 | +```powershell |
| 15 | +#Import-Module Az.Storage -MinimumVersion 1.7.0 -Scope Local |
| 16 | +Param( |
| 17 | + [Parameter(Mandatory=$True)][System.String] $ResourceGroupName, |
| 18 | + [Parameter(Mandatory=$True)][System.String] $StorageAccountName, |
| 19 | + [Parameter(Mandatory=$True)][System.String] $FileShareName, |
| 20 | + [Parameter(Mandatory=$True)][System.String] $SubscriptionId, |
| 21 | + [Parameter(Mandatory=$False)][System.Boolean] $ListOption, |
| 22 | + [Parameter(Mandatory=$False)][System.String] $DeletedShareVersion |
| 23 | + ) |
| 24 | +
|
| 25 | +Function Restore-DeletedFileShare |
| 26 | +{ |
| 27 | + Param( |
| 28 | + [Parameter(Mandatory=$True)][Microsoft.WindowsAzure.Commands.Common.Storage.LazyAzureStorageContext] $Context, |
| 29 | + [Parameter(Mandatory=$True)][System.String] $FileShareName, |
| 30 | + [Parameter(Mandatory=$False)][System.String] $DeletedShareVersion |
| 31 | + ) |
| 32 | +
|
| 33 | + if ([string]::IsNullOrWhiteSpace($FileShareName)) |
| 34 | + { |
| 35 | + Write-Error "Please specify the required input parameter: FileShareName" -ErrorAction Stop |
| 36 | + } |
| 37 | +
|
| 38 | + $FileShareName = $FileShareName.ToLowerInvariant() |
| 39 | +
|
| 40 | + Write-Verbose "Restoring a file share with the name: $FileShareName" -Verbose |
| 41 | +
|
| 42 | +
|
| 43 | + Write-Information -MessageData "Started: Creating SASToken to List File Shares" -InformationAction Continue |
| 44 | +
|
| 45 | + $listToken = New-AzStorageAccountSASToken -Context $Context -Service File -ResourceType Service -Permission "l" -Protocol HttpsOrHttp -StartTime (Get-Date).AddHours(-1) -ExpiryTime (Get-Date).AddHours(1) |
| 46 | +
|
| 47 | + Write-Information -MessageData "Completed: Creating SASToken to List File Shares" -InformationAction Continue |
| 48 | +
|
| 49 | + Write-Information -MessageData "Started: Listing File Shares to find the deleted file share" -InformationAction Continue |
| 50 | +
|
| 51 | + $listSharesUrl = [string]::Concat($Context.FileEndPoint, "?include=metadata,deleted&comp=list&api-version=2019-10-10&", $listToken.Substring(1)) |
| 52 | +
|
| 53 | + $listSharesResponse = Invoke-WebRequest $listSharesUrl -Method "GET" -Verbose |
| 54 | +
|
| 55 | + if ($listSharesResponse.StatusCode -ne 200) |
| 56 | + { |
| 57 | + Write-Error "Request to list file shares failed." -ErrorAction Stop |
| 58 | + } |
| 59 | +
|
| 60 | + Write-Verbose $listSharesResponse.RawContent -Verbose |
| 61 | +
|
| 62 | + $listSharesResponseContent = $listSharesResponse.Content.Substring(3) |
| 63 | +
|
| 64 | + Write-Information -MessageData "Completed: Listing File Shares to find the deleted file share" -InformationAction Continue |
| 65 | +
|
| 66 | + Write-Information -MessageData "Started: Search for a deleted file share with the specified name" -InformationAction Continue |
| 67 | +
|
| 68 | + $deletedFileShares = Select-Xml -Content $listSharesResponseContent -XPath "/EnumerationResults/Shares/Share[Deleted=""true"" and Name=""$FileShareName""]" |
| 69 | +
|
| 70 | + $matchedCount = 0 |
| 71 | + $deletedShareVersions = New-Object System.Collections.Generic.List[string] |
| 72 | +
|
| 73 | + foreach($share in $deletedFileShares) |
| 74 | + { |
| 75 | + if($matchedCount -eq 0) |
| 76 | + { |
| 77 | + Write-Verbose $share.Node.InnerXml -Verbose |
| 78 | +
|
| 79 | + Write-Information -MessageData "Completed: Search for a deleted file share with the specified name And Found versions" -InformationAction Continue |
| 80 | + } |
| 81 | +
|
| 82 | + $shareVer = $share.Node.Item("Version").InnerText |
| 83 | + $shareDelTime = $share.Node.Item("Properties").Item("DeletedTime").InnerText |
| 84 | + $retDays = $share.Node.Item("Properties").Item("RemainingRetentionDays").InnerText |
| 85 | +
|
| 86 | + $deletedShareVersions.Add($share.Node.Item("Version").InnerText) |
| 87 | +
|
| 88 | + Write-Information -MessageData "DeletedVersion: $shareVer, DeletedTime: $shareDelTime, RemainingRetentionDays: $retDays" -InformationAction Continue |
| 89 | +
|
| 90 | + $matchedCount++ |
| 91 | + } |
| 92 | +
|
| 93 | + if($ListOption -eq $True) |
| 94 | + { |
| 95 | + return; |
| 96 | + } |
| 97 | +
|
| 98 | + if ($matchedCount -eq 0) |
| 99 | + { |
| 100 | + Write-Error "Deleted file share with the specified name was not found." -ErrorAction Stop |
| 101 | + } |
| 102 | + elseif($matchedCount -eq 1 -and ([string]::IsNullOrWhiteSpace($DeletedShareVersion) -or $deletedShareVersions.Contains($DeletedShareVersion))) |
| 103 | + { |
| 104 | + $DeletedShareVersion = $deletedShareVersions |
| 105 | + } |
| 106 | + elseif ($matchedCount -gt 1) |
| 107 | + { |
| 108 | + if ([string]::IsNullOrWhiteSpace($DeletedShareVersion) -or !$deletedShareVersions.Contains($DeletedShareVersion)) |
| 109 | + { |
| 110 | + Write-Error "More than one share with the specified name was found. Please specify a valid DeletedShareVersion parameter from above possible values." -ErrorAction Stop |
| 111 | + } |
| 112 | + } |
| 113 | +
|
| 114 | + Write-Information -MessageData "Completed: Search for a deleted file share with the specified name And Found version: $DeletedShareVersion" -InformationAction Continue |
| 115 | +
|
| 116 | + Write-Information -MessageData "Started: Creating SASToken to Restore File Share" -InformationAction Continue |
| 117 | +
|
| 118 | + $restoreToken = New-AzStorageAccountSASToken -Context $Context -Service File -ResourceType Container -Permission "w" -Protocol HttpsOrHttp -StartTime (Get-Date).AddHours(-1) -ExpiryTime (Get-Date).AddHours(1) |
| 119 | +
|
| 120 | + Write-Information -MessageData "Completed: Creating SASToken to Restore File Share" -InformationAction Continue |
| 121 | +
|
| 122 | + Write-Information -MessageData "Started: Restore File Share" -InformationAction Continue |
| 123 | +
|
| 124 | + $restoreShareUrl = [string]::Concat($Context.FileEndPoint, $FileShareName, "?restype=share&comp=undelete&api-version=2019-10-10&", $restoreToken.Substring(1)) |
| 125 | +
|
| 126 | + $restoreHeaders = @{"x-ms-deleted-share-name" = $FileShareName; "x-ms-deleted-share-version" = $DeletedShareVersion} |
| 127 | +
|
| 128 | + $restoreResponse = Invoke-WebRequest $restoreShareUrl -Headers $restoreHeaders -Method "PUT" -Verbose |
| 129 | +
|
| 130 | + if ($restoreResponse.StatusCode -ne 201) |
| 131 | + { |
| 132 | + Write-Error "Request to restore a file share failed." -ErrorAction Stop |
| 133 | + } |
| 134 | +
|
| 135 | + Write-Verbose $restoreResponse.RawContent -Verbose |
| 136 | +
|
| 137 | + Write-Information -MessageData "Completed: Restore File Share" -InformationAction Continue |
| 138 | +} |
| 139 | +
|
| 140 | +Connect-AzAccount |
| 141 | +Select-AzSubscription -Subscription $SubscriptionId |
| 142 | +$sa = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName |
| 143 | +
|
| 144 | +
|
| 145 | +Restore-DeletedFileShare $sa.Context $FileShareName $DeletedShareVersion |
| 146 | +``` |
| 147 | + |
| 148 | +## How to use the script in different scenarios |
| 149 | + |
| 150 | +### Prerequisites |
| 151 | + |
| 152 | +1. Install the latest Azure PowerShell Az modules from [this link](https://docs.microsoft.com//powershell/azure/install-az-ps?view=azps-3.3.0) before running the script. |
| 153 | +2. Keep the following details handy as you'll need to pass them as values for different parameters of the script: |
| 154 | + |
| 155 | + * **-SubscriptionId** - ID of the subscription where the file share is present. |
| 156 | + * **-ResourceGroupName** - Resource Group of the Storage Account hosting the file share. |
| 157 | + * **-StorageAccountName** - Name of the storage account hosting the file share. |
| 158 | + * **-FileShareName** - Name of the file share to be undeleted |
| 159 | + |
| 160 | +### Execution steps |
| 161 | + |
| 162 | +1. Save the script above on your machine with a name of your choice. In this example, we saved it as *Undelete.ps1* |
| 163 | +2. Run the script according to the scenario that fits your requirements. |
| 164 | + |
| 165 | +#### Scenario 1 |
| 166 | + |
| 167 | +There are no multiple deleted versions with the same name as the file share you're trying to undelete. |
| 168 | + |
| 169 | +The following example undeletes the file share *share1* present in storage account *afsshare*. |
| 170 | + |
| 171 | +```powershell |
| 172 | + .\UnDelete.ps1 -ResourceGroupName afsshare -StorageAccountName afsshare -SubscriptionId f75d8d8b-6735-4697-82e1-1a7a3ff0d5d4 -FileShareName share1 |
| 173 | +``` |
| 174 | + |
| 175 | +The output should show the message `Completed:Restore File Share` |
| 176 | + |
| 177 | +#### Scenario 2 |
| 178 | + |
| 179 | +There are multiple deleted versions with the same name as the fileshare you're trying to undelete. |
| 180 | + |
| 181 | +The following example undeletes a version of the file share *share1* |
| 182 | + |
| 183 | +##### Step 1 |
| 184 | + |
| 185 | +Execute the script as follows by providing the file share name. |
| 186 | + |
| 187 | +```PowerShell |
| 188 | + .\UnDelete.ps1 -ResourceGroupName afsshare -StorageAccountName afsshare -SubscriptionId f75d8d8b-6735-4697-82e1-1a7a3ff0d5d4 -FileShareName share1 |
| 189 | +``` |
| 190 | + |
| 191 | +```Output |
| 192 | +Completed: Search for a deleted file share with the specified name and Found versions |
| 193 | +DeletedVersion: 01D5D7F77ACC7864, DeletedTime: Fri, 31 Jan 2020 05:30:33 GMT, RemainingRetentionDays: 14 |
| 194 | +DeletedVersion: 01D5D7F7A76CAF42, DeletedTime: Fri, 31 Jan 2020 05:31:25 GMT, RemainingRetentionDays: 14 |
| 195 | +Restore-DeletedFileShare : More than one share with the specified name was found. Please specify a valid DeletedShareVersion parameter from above possible values. |
| 196 | +``` |
| 197 | + |
| 198 | +##### Step 2 |
| 199 | + |
| 200 | +Choose the version from the output of step 1 that you want to undelete and pass it as a value for the **-DeletedShareVersion** parameter. |
| 201 | + |
| 202 | +The following example undeletes the *01D5D7F77ACC7864* version of the *share1* file share. |
| 203 | + |
| 204 | +```powershell |
| 205 | + .\UnDelete.ps1 -ResourceGroupName afsshare-StorageAccountName afsshare -SubscriptionId f75d8d8b-6735-4697-82e1-1a7a3ff0d5d4 -FileShareName share1 -DeletedShareVersion 01D5D7F77ACC7864 |
| 206 | +``` |
| 207 | + |
0 commit comments