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

[玩转系统] SharePoint Online:使用 PowerShell 搜索回收站

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

SharePoint Online:使用 PowerShell 搜索回收站


要求: 在 SharePoint Online 回收站中搜索特定用户删除的所有文件。

如何搜索SharePoint Online回收站?

您知道可以使用 PowerShell 搜索 SharePoint Online 中的回收站吗?在这篇博文中,我们将探讨使用 PowerShell 搜索 SharePoint 在线回收站的基础知识。

要查找特定用户删除的所有项目,请执行以下操作:

  1. 导航到您的 SharePoint Online 站点,然后单击站点设置齿轮 >> 选择“站点设置”。
  2. 在“网站设置”页面中,单击“网站集管理”中的“回收站”。此页面为您提供网站集中所有已删除的项目以及用户数据删除的项目。

    [玩转系统] SharePoint Online:使用 PowerShell 搜索回收站

但是,当您有大量已删除的文件时,通过 Web 用户界面搜索 SharePoint Online 回收站会很痛苦。因此,让我们使用 PowerShell 从回收站获取已删除的文件。

用于搜索 SharePoint Online 回收站的 PowerShell 脚本

以下是用于搜索回收站的 SharePoint Online PowerShell:


#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/Sales"
$AdminUserName="[email protected]"
$DeletedByUserAccount="[email protected]"

#Get the password to connect 
$Password = Read-host -assecurestring "Enter Password for $AdminUserName"
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($AdminUserName,$Password)
 
Try {    
    #Setup the context
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
    $Context.Credentials = $Credentials
    
    #Get the site recycle bin
    $Site = $Context.Site
    $RecycleBinItems = $Site.RecycleBin
    $Context.Load($Site)
    $Context.Load($RecycleBinItems)
    $Context.ExecuteQuery()

    #Get all items deleted by particular user in Recycle bin
    $DeletedByUser = $RecycleBinItems | Where {$_.DeletedByEmail -eq $DeletedByUserAccount}
    Write-Host "Total Number of Items deleted by user:" $DeletedByUser.Count
    
    #format output as table and print to console
    $DeletedByUser | Select Title, DeletedByEmail, DeletedDate | Format-Table
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

用于获取 SharePoint Online 回收站项目的 PnP PowerShell:

使用 PnP PowerShell 在 SharePoint Online 中搜索回收站非常简单。只需几行代码,您就可以查询回收站的内容并找到您要查找的项目。首先,打开 PowerShell 并连接到您的 SharePoint Online 网站。然后,运行以下脚本:

以下是如何使用 PnP PowerShell cmdlet Get-PnPRecycleBinItem 搜索 SharePoint Online 回收站。


#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Interactive

#Get Recycle bin Items
Get-PnPRecycleBinItem -RowLimit 500000 | Select Title, ItemType, Size, ItemState, DirName, DeletedByName, DeletedDate | Format-table -AutoSize

此 PowerShell 脚本从给定 SharePoint Online 网站集的回收站获取所有项目:

[玩转系统] SharePoint Online:使用 PowerShell 搜索回收站

您可以通过以下方式将回收站项目数据导出到 CSV 文件:


#Get Recycle bin Items and Export to CSV
Get-PnPRecycleBinItem -RowLimit 500000 | Select Title, ItemType, Size, ItemState, DirName, DeletedByName, DeletedDate | Export-Csv "C:\Temp\RecycleBin.csv" -NoTypeInformation

该脚本从第一阶段和第二阶段回收站获取所有已删除项目的数据。要从回收站恢复已删除的项目,请使用:如何在 SharePoint Online 中恢复已删除的项目?

如果您是 SharePoint Online 管理员,您还可以使用 PowerShell 来管理回收站。例如,您可以使用 PowerShell 清空回收站或从回收站还原项目。要了解有关将 PowerShell 与 SharePoint Online 结合使用的详细信息,请参阅以下文章:

  • 如何将 PowerShell 与 SharePoint Online 结合使用?
  • 如何清空SharePoint Online回收站?
  • 如何从 SharePoint Online 回收站恢复网站?

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

取消回复欢迎 发表评论:

关灯