ค้นหาเว็บไซต์

OneDrive for Business: กู้คืนไฟล์และโฟลเดอร์ทั้งหมดจากถังรีไซเคิลโดยใช้ PowerShell


ความต้องการ: กู้คืนไฟล์และโฟลเดอร์ที่ถูกลบทั้งหมดจาก OneDrive for Business โดยใช้ PowerShell

PowerShell เพื่อกู้คืนรายการที่ถูกลบทั้งหมดใน OneDrive for Business

OneDrive สำหรับธุรกิจจะจัดเก็บรายการที่ถูกลบทั้งหมดไว้ในถังรีไซเคิล หากคุณลบรายการโดยไม่ตั้งใจ คุณสามารถคืนค่ารายการนั้นได้จากถังรีไซเคิล การกู้คืนรายการที่ถูกลบจาก OneDrive for Business Recycle Bin ผ่านทางอินเทอร์เฟซของเว็บเบราว์เซอร์ได้อธิบายไว้ในโพสต์อื่น: วิธีคืนค่าไฟล์และโฟลเดอร์จาก OneDrive ในโพสต์นี้ เรามาดูวิธีใช้ PowerShell เพื่อกู้คืนไฟล์และโฟลเดอร์ที่ถูกลบทั้งหมดจากถังรีไซเคิล

โปรดทราบว่า คุณต้องมีสิทธิ์ของผู้ดูแลระบบไซต์คอลเลกชันในไซต์ OneDrive ก่อนที่จะดำเนินการคืนค่า


#Parameters
$AdminCenterURL="https://crescent-admin.sharepoint.com"
$UserAccount = "Salaudeen@Crescent.com" #UPN

Try {
    #Connect to Admin Center
    Connect-PnPOnline -Url $AdminCenterURL -Interactive

    #Get onedrive site URL of the user
    $OneDriveURL = Get-PnPUserProfileProperty -Account $UserAccount | Select -ExpandProperty PersonalUrl

    If($OneDriveURL -ne $null)
    {
        #Connect to OneDrive site
        Connect-PnPOnline -Url $OneDriveURL -Interactive

        #Get All items in the recycle bin
        $RecycleBinItems = Get-PnPRecycleBinItem -RowLimit 500000

        #Check if there are any deleted items in recycle bins
        If($RecycleBinItems.count -eq 0) {
            Write-host "No Items found in the recycle bin!" -f Yellow
            Break
        }

        #Restore all items from the recycle bin
        $RecycleBinItems = Get-PnPRecycleBinItem -RowLimit 500000
        ForEach($Item in $RecycleBinItems)
        {
            #Get the Original location of the deleted file or folder
            $OriginalLocation = "/"+$Item.DirName+"/"+$Item.LeafName
            If($Item.ItemType -eq "File")
            {
                $OriginalItem = Get-PnPFile -Url $OriginalLocation -AsListItem -ErrorAction SilentlyContinue
            }
            Else #Folder
            {
                $OriginalItem = Get-PnPFolder -Url $OriginalLocation -ErrorAction SilentlyContinue
            }
            #Check if the item exists in the original location
            If($OriginalItem -eq $null)
            { 
                #Restore the item
                Restore-PnPRecycleBinItem -Identity $Item -Force
                Write-host "Restored Item '$($Item.LeafName)' to '$($Item.DirName)'" -f Green
            }
            Else
            {
                Write-Host "A file or folder with this name $($Item.LeafName) already exists in '$($Item.DirName)', Skipping.."  -f Yellow
            }
        }
    }
    Else
    {
        Write-host "OneDrive site for the user doesn't exist!" -f Yellow
    }
}
Catch {
    Write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

โพสต์ที่เกี่ยวข้อง: กู้คืนไฟล์ที่ถูกลบจากถังรีไซเคิลใน SharePoint Online โดยใช้ PowerShell

บทความที่เกี่ยวข้อง