当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] SharePoint Online:使用 PowerShell 从回收站恢复已删除的子网站

作者:精品下载站 日期:2024-12-14 14:49:26 浏览:17 分类:玩电脑

SharePoint Online:使用 PowerShell 从回收站恢复已删除的子网站


要求:用户错误地删除了子网站。现在的要求是恢复 SharePoint Online 中已删除的子网站。

如何在 SharePoint 中恢复已删除的子网站?

您或最终用户是否曾经意外删除过 SharePoint Online 中的子网站?是否有拥有完全控制权限的人误按了删除按钮? (在现代团队网站和通信网站中,所有具有完全控制权限的最终用户都可以使用它)。如果是这样,请不要担心 - SharePoint Online 中删除的子网站将移至第二阶段回收站,并且可以由网站集管理员恢复。此博文将引导您完成在 SharePoint Online 中恢复已删除子网站所需的步骤。我们将使用 SharePoint 回收站和 PowerShell 方法来恢复子网站。

要在 SharePoint 中恢复已删除的子网站,请执行以下步骤:

  1. 以网站集管理员身份登录 SharePoint Online 网站集
  2. 单击设置齿轮图标>>站点信息>>查看所有站点设置
  3. 点击网站集管理下的“回收站”即可进入网站集回收站。
  4. 单击“第二阶段回收站”链接,然后通过单击子网站的复选框来选择所需的子网站(第二阶段回收站 URL:https://.sharepoint.com/sites/Marketing/_layouts /15/AdminRecycleBin.aspx?view=13)
  5. 单击命令栏中的“恢复”按钮开始恢复子网站,您的子网站将立即备份并运行。

[玩转系统] SharePoint Online:使用 PowerShell 从回收站恢复已删除的子网站

默认情况下,已删除的 SharePoint 网站将在回收站中保留 93 天。如果您在回收站中找不到您的子网站 - 您的网站可能已被完全删除!因此,您只能在 SharePoint 本地环境中还原内容数据库备份,或者向 Microsoft 提出 SharePoint Online 的支持票证。在现代网站/群组网站上,恢复网站的说明可能略有不同。

使用 PowerShell 从回收站恢复已删除的子网站

如何使用 PowerShell 恢复已删除的子网站?以下是如何使用 PowerShell 在 SharePoint Online 中恢复已删除的子网站:


#Load SharePoint Online Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
#Variables for Processing
$SiteUrl = "https://crescent.sharepoint.com/sites/Marketing"
$DeletedSubsiteURL="/sites/marketing/2018/docs"
 
#Get the credentials to connect 
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
Try {    
    #Setup the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $Credentials
     
    #Get the Admin recycle bin
    $Site = $Context.Site
    $RecycleBinItems = $Site.RecycleBin
    $Context.Load($Site)
    $Context.Load($RecycleBinItems)
    $Context.ExecuteQuery()
 
    #Frame DirName (Parent Site URL) and LeafName (Subsite URL)
    $DirName=$DeletedSubsiteURL.Substring(1,$DeletedSubsiteURL.LastIndexOf("/")-1)
    $LeafName=$DeletedSubsiteURL.Substring($DeletedSubsiteURL.LastIndexOf("/")+1)

    #Get the deleted subsite from Recycle bin
    $DeletedSubsite = $RecycleBinItems | Where { ($_.DirName -eq $DirName) -and ($_.LeafName -eq $LeafName)}
    If($DeletedSubSite -ne $Null)
    { 
        #Restore the first subsite found from Recylce bin
        $DeletedSubSite[0].Restore()
        $Context.ExecuteQuery()
        Write-Host -f Green "Deleted Subsite restored Successfully!"
    }
    else
    {
        Write-Host -f Yellow "Deleted Subsite Not Found in Recycle Bin!"
    }
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

一旦子网站恢复到其目的地,您将能够访问它以及任何关联的网站内容及其原始文件,就像以前一样。

SharePoint Online:PnP PowerShell 恢复已删除的子站点

让我们使用 PnP PowerShell 命令 Restore-PnPRecycleBinItem 来还原子网站 SharePoint Online。此 PowerShell 脚本首先连接到 SharePoint Online 网站。然后,使用“Get-PnPRecycleBinItem”cmdlet 获取所有已删除的子网站,包括要恢复的子网站。最后,使用“Restore-PnPRecycleBinItem”PowerShell cmdlet 恢复子站点。只需指定子站点的 URL 并运行脚本即可。子网站将恢复到其原始位置。


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/Marketing"
$SiteTitle="Branding"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get Deleted Subsite
$DeletedSite = Get-PnPRecycleBinItem -RowLimit 500000 | Where { $_.Title -eq $SiteTitle -and $_.ItemType -eq "Web"}

#Restore Subsite
If($DeletedSite)
{
    Restore-PnPRecycleBinItem -Identity $DeletedSite -Force -ErrorAction Stop
    Write-Host -f Green "Subsite '$SiteTitle' restored successfully!"
}
Else
{
    Write-host -f Yellow "Could not Find deleted Subsite:"$SiteTitle
} 

恢复子网站后,进入子网站URL验证是否恢复成功。如果子网站已成功恢复,您应该能够访问它并查看删除子网站之前存在的所有内容和设置。

若要从 SharePoint 管理中心或 PowerShell 还原 SharePoint Online 中的 SharePoint 网站集,请使用:SharePoint Online:如何使用 PowerShell 还原已删除的网站集?

经常问的问题:

如何在 SharePoint Online 中恢复文档?

要恢复 SharePoint Online 中的文档,请按照下列步骤操作: 登录到 SharePoint Online 网站,单击“设置”图标 >> 选择“网站内容”,然后单击“回收站”链接。在“回收站”页面上选择要还原的每个项目,然后单击“还原”按钮。
详细信息:SharePoint Online:使用 PowerShell 从回收站还原已删除的项目

如何恢复 SharePoint 上已删除的文件夹?

要恢复 SharePoint Online 文档库中已删除的文件夹,请导航到您的 SharePoint 网站 >> 单击“设置”齿轮,然后选择“网站内容”链接。在网站内容页面上,单击回收站链接。选择要从回收站还原的文件夹,然后单击工具栏中的“还原”按钮。
详细信息:使用 PowerShell 在 SharePoint Online 中还原已删除的文件夹

如何使用 PowerShell 删除 SharePoint Online 中的子网站?

使用“$web.DeleteObject()”CSOM 方法删除 Sharepoint Online 中的子网站。通过 PnP PowerShell,您可以使用“Remove-PnPWeb”cmdlet。
详细信息:PowerShell 用于删除 SharePoint Online 中的子网站

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯