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

คืนค่ารายการที่ถูกลบจากถังรีไซเคิลใน SharePoint โดยใช้ PowerShell


จะคืนค่ารายการที่ถูกลบจากถังรีไซเคิลของ SharePoint ได้อย่างไร

PowerShell เป็นภาษาสคริปต์ที่มีประสิทธิภาพซึ่งช่วยให้ผู้ดูแลระบบสามารถควบคุมสภาพแวดล้อม SharePoint ของตนได้อย่างมาก ถ้าคุณเป็นเหมือนผู้ดูแลระบบ SharePoint ส่วนใหญ่ คุณจะต้องคืนค่ารายการที่ถูกลบออกจากไซต์ของคุณเป็นครั้งคราว ใน SharePoint เมื่อรายการถูกลบ รายการนั้นจะถูกย้ายไปยังถังรีไซเคิล จากนั้น คุณสามารถกู้คืนได้หากคุณเปลี่ยนใจหรือถูกลบโดยไม่ได้ตั้งใจ โพสต์ในบล็อกนี้จะแสดงวิธีใช้ PowerShell เพื่อกู้คืนรายการที่ถูกลบใน SharePoint

หากต้องการคืนค่ารายการที่ถูกลบ ให้ทำตามขั้นตอนเหล่านี้:

  • นำทางไปยังไซต์ SharePoint ของคุณ >> คลิกที่ลิงก์ "ถังรีไซเคิล" บนเปิดใช้ด่วน
  • ค้นหาออบเจ็กต์ที่คุณต้องการคืนค่าโดยเลือกกล่องกาเครื่องหมายถัดจากออบเจ็กต์ จากนั้นคลิกคืนค่าการเลือก

SharePoint: กู้คืนจากถังรีไซเคิลโดยใช้ PowerShell

ต่อไปนี้เป็นวิธีใช้ PowerShell เพื่อกู้คืนรายการจากถังรีไซเคิล:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration variables
$SiteURL="https://portal.crescent.com/sites/ops/"
$ItemName="Classifieds.xlsx" #Can be a List Name, File Name or ID of an Item

#Get Objects
$site = Get-SPSite $SiteURL
$RecycleBin = $site.RecycleBin

#Get the Item from Recycle bin
$Item = $RecycleBin | Where{$_.Title -eq $ItemName}

if($Item -ne $null)
{
 $Item.Restore()
 Write-Host "Item Restored from Recycle Bin!" -f DarkGreen
}
else
{
 Write-Host "No Item Found with the given name!" -ForegroundColor RED
}

สคริปต์นี้จะเรียกคืนรายการที่กำหนดจากถังรีไซเคิล จะเกิดอะไรขึ้นหากไฟล์ใดไฟล์หนึ่งถูกลบมากกว่าหนึ่งครั้ง? จะเกิดอะไรขึ้นหากตำแหน่งดั้งเดิมของไฟล์มีไฟล์อื่นที่มีชื่อเดียวกัน


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Configuration variables
$SiteURL="https://intranet.crescent.com"
$ItemName="Juneau.docx" 

#Get necessary objects
$Site = Get-SPSite $SiteURL
$Web = Get-SPWeb $SiteURL
$RecycleBin = $Site.RecycleBin
 
#Get the latest Item deleted from Recycle bin
$DeletedItem = $RecycleBin | Where {$_.Title -eq $ItemName} | Sort-Object DeletedDate -Descending | Select -First 1

If($DeletedItem -ne $Null)
{
    #Get the Original location of the deleted file
    $OriginalLocation = $DeletedItem.DirName+"/"+$DeletedItem.LeafName

    #Check if file exists
    If(!$Web.GetFile($OriginalLocation).Exists)
    { 
        $DeletedItem.Restore()
        Write-Host "Deleted File restored Successfully!" -f Green
    }
    else
    {
        Write-Host "There is another item with the same name!" -f Yellow
    }
}
Else
{
    Write-Host "No Item Found with the given name!" -f Yellow
}

สคริปต์นี้รับไฟล์ล่าสุดจากถังรีไซเคิลตามวันที่ลบ และตรวจสอบว่าตำแหน่งเดิมมีไฟล์ชื่อเดียวกันหรือไม่ ถ้าไม่เช่นนั้นจะกู้คืนไฟล์ มิฉะนั้นจะข้ามไป

เคล็ดลับ: จะค้นหาและกู้คืนรายการทั้งหมดที่ถูกลบโดยผู้ใช้รายใดรายหนึ่งได้อย่างไร ใช้: $RecycleBin | โดยที่ { $_.DeletedBy -like “Domain\Account” }

กู้คืนตามประเภทออบเจ็กต์ (เช่น รายการ เว็บ รายการ ฯลฯ)

จะทำอย่างไรถ้าคุณต้องการคืนค่ารายการที่ถูกลบหรือไซต์ย่อยที่ถูกลบทั้งหมดจากถังรีไซเคิล นี่คือสคริปต์ PowerShell เพื่อกู้คืนรายการที่ถูกลบทั้งหมดจากถังรีไซเคิล SharePoint 2013


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Configuration variables
$SiteURL="https://portal.crescent.com"

#Get Objects
$site = Get-SPSite $SiteURL
$RecycleBin = $site.RecycleBin

#Get All deleted Lists from Recycle bin
$DeletedItems = $RecycleBin | Where{ $_.ItemType -eq "List"}
if($DeletedItems)
{
 Foreach($Item in $DeletedItems)
 {
  $Item.Restore()
  Write-Host "'$($Item.Title)' Restored from Recycle Bin!" -f DarkGreen
 }
}

PowerShell เพื่อกู้คืนรายการทั้งหมดจาก SharePoint Recycle Bin:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

$SiteURL="https://portal.crescent.com/sites/operations"
$Site = Get-SPSite $SiteURL

#Get All Recycle bin items
$DeletedItems = $Site.RecycleBin

If($DeletedItems)
{
    ForEach($Item in $DeletedItems) 
    { 
         $Site.RecycleBin.restore($Item.ID)
        Write-Host "Item restored:"$Item.Title
     }
}

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

มาคืนค่าไฟล์ทั้งหมดจากถังรีไซเคิลของ SharePoint ข้ามไปหากมีไฟล์ชื่อเดียวกันอยู่ในตำแหน่งเดิม


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
#Parameter
$SiteURL="https://sharepoint.crescent.com"

#Get necessary objects
$Site = Get-SPSite $SiteURL
$Web = Get-SPWeb $SiteURL
$RecycleBin = $Site.RecycleBin
 
#Get deleted Items from Recycle bin - sorted by deleted date in Descending order
$DeletedItems = $RecycleBin | Where {$_.ItemType -eq "File"} | Sort-Object DeletedDate -Descending

$DeletedItems | ForEach-Object {
    #Get the Original location of the deleted file
    $OriginalLocation = $_.DirName+"/"+$_.LeafName

    #Check if file exists
    If(!$Web.GetFile($OriginalLocation).Exists)
    { 
        $_.Restore()
        Write-Host "$($_.LeafName) restored Successfully!" -f Green
    }
    Else
    {
        Write-Host "There is another file with the same name.. Skipping $($_.LeafName)" -f Yellow
    }
}

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

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