برنامج PowerShell النصي لإلغاء حذف مشاركة ملف محذوف عن طريق الخطأ

يساعدك هذا البرنامج النصي على إلغاء حذف مشاركة ملف، إذا قمت بحذفه عن طريق الخطأ. توفر لك ميزة أمان الحذف المبدئي لمشاركات الملفات خيار إلغاء حذف مشاركة ملف خلال فترة الاستبقاء بـ 14 يومًا، ما يسمح باسترداد جميع محتويات مشاركة الملفات واللقطات ونقاط الاسترداد. لمعرفة مزيد حول الحذف المبدئي، يرجى زيارة هذا ⁧⁩الرابط⁧⁩.

نموذج للبرنامج النصي

#Import-Module Az.Storage -MinimumVersion 1.7.0 -Scope Local
Param(
        [Parameter(Mandatory=$True)][System.String] $ResourceGroupName,
        [Parameter(Mandatory=$True)][System.String] $StorageAccountName,
        [Parameter(Mandatory=$True)][System.String] $FileShareName,
        [Parameter(Mandatory=$True)][System.String] $SubscriptionId,
        [Parameter(Mandatory=$False)][System.Boolean] $ListOption,
        [Parameter(Mandatory=$False)][System.String] $DeletedShareVersion
    )

Function Restore-DeletedFileShare
{
    Param(
        [Parameter(Mandatory=$True)][Microsoft.WindowsAzure.Commands.Common.Storage.LazyAzureStorageContext] $Context,
        [Parameter(Mandatory=$True)][System.String] $FileShareName,
        [Parameter(Mandatory=$False)][System.String] $DeletedShareVersion
        )

    if ([string]::IsNullOrWhiteSpace($FileShareName))
    {
        Write-Error "Please specify the required input parameter: FileShareName" -ErrorAction Stop
    }

    $FileShareName = $FileShareName.ToLowerInvariant()

    Write-Verbose "Restoring a file share with the name: $FileShareName" -Verbose


    Write-Information -MessageData "Started: Creating SASToken to List File Shares" -InformationAction Continue

    $listToken = New-AzStorageAccountSASToken -Context $Context -Service File -ResourceType Service -Permission "l" -Protocol HttpsOrHttp -StartTime (Get-Date).AddHours(-1) -ExpiryTime (Get-Date).AddHours(1)

    Write-Information -MessageData "Completed: Creating SASToken to List File Shares" -InformationAction Continue

    Write-Information -MessageData "Started: Listing File Shares to find the deleted file share" -InformationAction Continue

    $listSharesUrl = [string]::Concat($Context.FileEndPoint, "?include=metadata,deleted&comp=list&api-version=2019-10-10&", $listToken.Substring(1))

    $listSharesResponse = Invoke-WebRequest $listSharesUrl -Method "GET" -Verbose

    if ($listSharesResponse.StatusCode -ne 200)
    {
        Write-Error "Request to list file shares failed." -ErrorAction Stop
    }

    Write-Verbose $listSharesResponse.RawContent -Verbose

    $listSharesResponseContent = $listSharesResponse.Content.Substring(3)

    Write-Information -MessageData "Completed: Listing File Shares to find the deleted file share" -InformationAction Continue

    Write-Information -MessageData "Started: Search for a deleted file share with the specified name" -InformationAction Continue

    $deletedFileShares = Select-Xml -Content $listSharesResponseContent -XPath "/EnumerationResults/Shares/Share[Deleted=""true"" and Name=""$FileShareName""]"

    $matchedCount = 0
    $deletedShareVersions = New-Object System.Collections.Generic.List[string]

    foreach($share in $deletedFileShares)
    {
        if($matchedCount -eq 0)
        {
          Write-Verbose $share.Node.InnerXml -Verbose

          Write-Information -MessageData "Completed: Search for a deleted file share with the specified name And Found versions" -InformationAction Continue
        }

        $shareVer = $share.Node.Item("Version").InnerText
        $shareDelTime = $share.Node.Item("Properties").Item("DeletedTime").InnerText
        $retDays = $share.Node.Item("Properties").Item("RemainingRetentionDays").InnerText

        $deletedShareVersions.Add($share.Node.Item("Version").InnerText)

        Write-Information -MessageData "DeletedVersion: $shareVer, DeletedTime: $shareDelTime, RemainingRetentionDays: $retDays"  -InformationAction Continue

        $matchedCount++
    }

    if($ListOption -eq $True)
    {
       return;
    }

    if ($matchedCount -eq 0)
    {
        Write-Error "Deleted file share with the specified name was not found." -ErrorAction Stop
    }
    elseif($matchedCount -eq 1 -and ([string]::IsNullOrWhiteSpace($DeletedShareVersion) -or $deletedShareVersions.Contains($DeletedShareVersion)))
    {
      $DeletedShareVersion = $deletedShareVersions
    }
    elseif ($matchedCount -gt 1)
    {
      if ([string]::IsNullOrWhiteSpace($DeletedShareVersion) -or !$deletedShareVersions.Contains($DeletedShareVersion))
      {
        Write-Error "More than one share with the specified name was found. Please specify a valid DeletedShareVersion parameter from above possible values." -ErrorAction Stop
      }
    }

    Write-Information -MessageData "Completed: Search for a deleted file share with the specified name And Found version: $DeletedShareVersion" -InformationAction Continue

    Write-Information -MessageData "Started: Creating SASToken to Restore File Share" -InformationAction Continue

    $restoreToken = New-AzStorageAccountSASToken -Context $Context -Service File -ResourceType Container -Permission "w" -Protocol HttpsOrHttp -StartTime (Get-Date).AddHours(-1) -ExpiryTime (Get-Date).AddHours(1)

    Write-Information -MessageData "Completed: Creating SASToken to Restore File Share" -InformationAction Continue

    Write-Information -MessageData "Started: Restore File Share" -InformationAction Continue

 $restoreShareUrl = [string]::Concat($Context.FileEndPoint, $FileShareName, "?restype=share&comp=undelete&api-version=2019-10-10&", $restoreToken.Substring(1))

    $restoreHeaders = @{"x-ms-deleted-share-name" = $FileShareName; "x-ms-deleted-share-version" = $DeletedShareVersion}

    $restoreResponse = Invoke-WebRequest $restoreShareUrl -Headers $restoreHeaders -Method "PUT" -Verbose

    if ($restoreResponse.StatusCode -ne 201)
    {
        Write-Error "Request to restore a file share failed." -ErrorAction Stop
    }

    Write-Verbose $restoreResponse.RawContent -Verbose

    Write-Information -MessageData "Completed: Restore File Share" -InformationAction Continue
}

Connect-AzAccount
Select-AzSubscription -Subscription $SubscriptionId
$sa = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName


Restore-DeletedFileShare $sa.Context $FileShareName $DeletedShareVersion

كيفية استخدام البرنامج النصي في سيناريوهات مختلفة

المتطلبات الأساسية

  1. يمكنك تثبيت أحدث الوحدات النمطية Azure PowerShell Az من ⁧⁩هذا الرابط⁧⁩ قبل تشغيل البرنامج النصي.

  2. احتفظ بالتفاصيل الآتية المفيدة حيث ستحتاج إلى تمريرها بوصفها قيمًا لمعلمات مختلفة من البرنامج النصي:

    • ⁩‎-SubscriptionId⁧⁩ - معرف الاشتراك حيثما توجد مشاركة الملف.
    • ⁩‎-‎ResourceGroupName⁧⁩ - مجموعة موارد حساب التخزين التي تستضيف مشاركة الملف.
    • ⁩‎-StorageAccountName⁧⁩ - اسم حساب التخزين الذي يستضيف مشاركة الملف.
    • ⁩‎-FileShareName⁧⁩ - اسم مشاركة الملف المطلوب إلغاء حذفه

خطوات التنفيذ

  1. احفظ البرنامج النصي أعلاه على جهازك باسم من اختيارك. في هذا المثال، حفظناه بوصفه ⁧⁩Undelete.ps1⁧
  2. تشغيل البرنامج النصي وفق للسيناريو الذي يناسب المتطلبات الخاصة بك.

السيناريو 1

لا توجد إصدارات متعددة تم حذفها باسم مشاركة الملف نفسه الذي تحاول إلغاء حذفه.

المثال الآتي يقوم بإلغاء حذف مشاركة الملف ⁧⁩share1⁧⁩ الموجود في حساب التخزين ⁧⁩afsshare⁧⁩.

   .\UnDelete.ps1 -ResourceGroupName afsshare -StorageAccountName afsshare -SubscriptionId f75d8d8b-6735-4697-82e1-1a7a3ff0d5d4 -FileShareName share1

يجب أن يظهر الإخراج الرسالة ⁧Completed:Restore File Share

السيناريو 2

هناك عدة إصدارات محذوفة تحمل اسم مشاركة الملف نفسه الذي تحاول إلغاء حذفه.

المثال التالي يقوم بإلغاء حذف إصدار من مشاركة الملف ⁧⁩share1⁧

الخطوة 1

يمكنك تنفيذ البرنامج النصي كما يأتي من خلال توفير اسم مشاركة الملف.

   .\UnDelete.ps1 -ResourceGroupName afsshare -StorageAccountName afsshare -SubscriptionId f75d8d8b-6735-4697-82e1-1a7a3ff0d5d4 -FileShareName share1
Completed: Search for a deleted file share with the specified name and Found versions
DeletedVersion: 01D5D7F77ACC7864, DeletedTime: Fri, 31 Jan 2020 05:30:33 GMT, RemainingRetentionDays: 14
DeletedVersion: 01D5D7F7A76CAF42, DeletedTime: Fri, 31 Jan 2020 05:31:25 GMT, RemainingRetentionDays: 14
Restore-DeletedFileShare : More than one share with the specified name was found. Please specify a valid DeletedShareVersion parameter from above possible values.
الخطوة 2

اختر الإصدار من إخراج الخطوة 1 الذي تريد إلغاء حذفه وتمريره بوصفه قيمة للمعلمة ⁧⁩‎-DeletedShareVersion⁧⁩.

المثال التالي يقوم بإلغاء حذف إصدار ⁧⁩01D5D7F77ACC7864⁧⁩ من مشاركة الملف ⁧⁩share1⁧⁩.

   .\UnDelete.ps1 -ResourceGroupName afsshare-StorageAccountName afsshare -SubscriptionId f75d8d8b-6735-4697-82e1-1a7a3ff0d5d4 -FileShareName share1 -DeletedShareVersion 01D5D7F77ACC7864