对目标集合使用内容搜索

Microsoft Purview 合规门户中的内容搜索工具不提供在 UI 中搜索 Exchange 邮箱或 SharePoint 中的特定文件夹的直接方法,OneDrive for Business网站。 但是,可以通过在实际搜索查询语法中指定电子邮件的文件夹 ID 属性或路径 (DocumentLink) 属性来搜索特定文件夹 (称为 目标集合) 。 当你确信对事例或特权项目做出响应的项目位于特定邮箱或网站文件夹中时,使用内容搜索执行目标集合非常有用。

可以使用本文中的脚本获取邮箱文件夹的文件夹 ID 或 SharePoint 和 OneDrive for Business 网站上文件夹 (DocumentLink) 路径。 然后,可以在搜索查询中使用文件夹 ID 或路径来返回位于 文件夹中的项目。

注意

若要返回位于 SharePoint 或OneDrive for Business网站的文件夹中的内容,本主题中的脚本使用 DocumentLink 托管属性而不是 Path 属性。 DocumentLink 属性比 Path 属性更可靠,因为它将返回文件夹中的所有内容,而 Path 属性不会返回某些媒体文件。

提示

如果你不是 E5 客户,请使用为期 90 天的 Microsoft Purview 解决方案试用版来探索其他 Purview 功能如何帮助组织管理数据安全性和合规性需求。 立即从Microsoft Purview 合规门户试用中心开始。 了解有关 注册和试用条款的详细信息。

运行目标集合之前

  • 你必须是合规性门户中电子数据展示管理器角色组的成员,才能在步骤 1 中运行脚本。 有关详细信息,请参阅分配电子数据展示权限

  • 还必须在Exchange Online组织中分配邮件收件人角色。 这是运行 脚本中包含的 Get-MailboxFolderStatistics cmdlet 所必需的。 默认情况下,邮件收件人角色分配给 Exchange Online 中的组织管理和收件人管理角色组。 有关在 Exchange Online 中分配权限的详细信息,请参阅管理角色组成员。 还可以创建自定义角色组,向其分配邮件收件人角色,然后在步骤 1 中添加需要运行脚本的成员。 有关详细信息,请参阅 管理角色组

  • 本文中的脚本支持新式身份验证。 如果你是 Microsoft 365 或 Microsoft 365 GCC 组织,则可以按原样使用脚本。 如果你是Office 365德国组织、Microsoft 365 GCC High 组织或 Microsoft 365 DoD 组织,则必须编辑脚本才能成功运行它。 具体而言,必须编辑该行Connect-ExchangeOnline,并使用 ExchangeEnvironmentName 参数 (以及组织类型) 的相应值来连接到 powerShell Exchange Online。 此外,还必须编辑该行 Connect-IPPSSession ,并使用 ConnectionUriAzureADAuthorizationEndpointUri 参数 (以及组织类型) 的相应值来连接到安全性 & 合规性 PowerShell。 有关详细信息,请参阅连接到Exchange Online PowerShell连接到安全性 & 合规性 PowerShell 中的示例。

  • 脚本中的文件夹 ID 必须采用十六进制 (十六进制) 值格式。

  • 每次运行脚本时,都会创建新的远程 PowerShell 会话。 这意味着可以使用所有可用的远程 PowerShell 会话。 若要防止这种情况发生,请运行以下命令以断开活动远程 PowerShell 会话的连接。

    Get-PSSession | Remove-PSSession; Disconnect-ExchangeOnline
    

    有关详细信息,请参阅使用远程 PowerShell 连接到 Exchange Online

  • 该脚本包含最少的错误处理。 该脚本的主要用途是快速显示邮箱文件夹 ID 或网站路径的列表,这些列表可用于内容搜索的搜索查询语法来执行目标集合。

  • 本文中提供的示例脚本在任何 Microsoft 标准支持程序或服务下都不受支持。 示例脚本“原样”提供,不提供任何形式的保证。 Microsoft 进一步拒绝所有默示保证,包括但不限于针对特定用途的适销性或适用性的任何默示保证。 由于示例脚本及文档的使用或性能所引起的全部风险均由你承担。 在任何情况下,对于由于使用或者无法使用示例脚本或文档所引起的任何损失(包括但不限于商业利润损失、业务中断、商业信息丢失或者其他经济损失),Microsoft、其作者或者参与创建、制作或交付脚本的任何人概不负责,即使 Microsoft 已被告知可能会出现此类损失。

步骤 1:运行脚本以获取邮箱或站点的文件夹列表

在第一步中运行的脚本将返回邮箱文件夹或 SharePoint 和OneDrive for Business文件夹的列表,以及每个文件夹的相应文件夹 ID 或路径。 运行此脚本时,它会提示你提供以下信息。

  • Email地址或站点 URL:键入保管人的电子邮件地址以返回 Exchange 邮箱文件夹和文件夹 ID 的列表。 或者键入 SharePoint 网站或OneDrive for Business网站的 URL,以返回指定网站的路径列表。 下面是一些示例:

    • Exchangestacig@contoso.onmicrosoft.com
    • SharePointhttps://contoso.sharepoint.com/sites/marketing
    • OneDrive for Businesshttps://contoso-my.sharepoint.com/personal/stacig_contoso_onmicrosoft_com
  • 用户凭据:该脚本将使用你的凭据连接到 Exchange Online PowerShell 或安全性 & 符合性 PowerShell(使用新式身份验证)。 如前所述,必须分配适当的权限才能成功运行此脚本。

若要显示邮箱文件夹列表或站点文档链接 (路径) 名称:

  1. 使用 .ps1 的文件名后缀将以下文本保存到Windows PowerShell脚本文件,GetFolderSearchParameters.ps1例如 。

    #########################################################################################################
    # This PowerShell script will prompt you for:                                #
    #    * Admin credentials for a user who can run the Get-MailboxFolderStatistics cmdlet in Exchange    #
    #      Online and who is an eDiscovery Manager in the compliance portal.            #
    # The script will then:                                            #
    #    * If an email address is supplied: list the folders for the target mailbox.            #
    #    * If a SharePoint or OneDrive for Business site is supplied: list the documentlinks (folder paths) #
    #    * for the site.                                                                                    #
    #    * In both cases, the script supplies the correct search properties (folderid: or documentlink:)    #
    #      appended to the folder ID or documentlink to use in a Content Search.                #
    # Notes:                                                #
    #    * For SharePoint and OneDrive for Business, the paths are searched recursively; this means the     #
    #      the current folder and all sub-folders are searched.                        #
    #    * For Exchange, only the specified folder will be searched; this means sub-folders in the folder    #
    #      will not be searched.  To search sub-folders, you need to use the specify the folder ID for    #
    #      each sub-folder that you want to search.                                #
    #    * For Exchange, only folders in the user's primary mailbox will be returned by the script.        #
    #########################################################################################################
    # Collect the target email address or SharePoint Url
    $addressOrSite = Read-Host "Enter an email address or a URL for a SharePoint or OneDrive for Business site"
    # Authenticate with Exchange Online and the compliance portal (Exchange Online Protection - EOP)
    if ($addressOrSite.IndexOf("@") -ige 0)
    {
       # List the folder Ids for the target mailbox
       $emailAddress = $addressOrSite
       # Connect to Exchange Online PowerShell
       if (!$ExoSession)
       {
           Import-Module ExchangeOnlineManagement
           Connect-ExchangeOnline -ShowBanner:$false -CommandName Get-MailboxFolderStatistics
       }
       $folderQueries = @()
       $folderStatistics = Get-MailboxFolderStatistics $emailAddress
       foreach ($folderStatistic in $folderStatistics)
       {
           $folderId = $folderStatistic.FolderId;
           $folderPath = $folderStatistic.FolderPath;
           $encoding= [System.Text.Encoding]::GetEncoding("us-ascii")
           $nibbler= $encoding.GetBytes("0123456789ABCDEF");
           $folderIdBytes = [Convert]::FromBase64String($folderId);
           $indexIdBytes = New-Object byte[] 48;
           $indexIdIdx=0;
           $folderIdBytes | select -skip 23 -First 24 | %{$indexIdBytes[$indexIdIdx++]=$nibbler[$_ -shr 4];$indexIdBytes[$indexIdIdx++]=$nibbler[$_ -band 0xF]}
           $folderQuery = "folderid:$($encoding.GetString($indexIdBytes))";
           $folderStat = New-Object PSObject
           Add-Member -InputObject $folderStat -MemberType NoteProperty -Name FolderPath -Value $folderPath
           Add-Member -InputObject $folderStat -MemberType NoteProperty -Name FolderQuery -Value $folderQuery
           $folderQueries += $folderStat
       }
       Write-Host "-----Exchange Folders-----"
       $folderQueries |ft
    }
    elseif ($addressOrSite.IndexOf("http") -ige 0)
    {
       $searchName = "SPFoldersSearch"
       $searchActionName = "SPFoldersSearch_Preview"
       $rawUrls = @()
       # List the folders for the SharePoint or OneDrive for Business Site
       $siteUrl = $addressOrSite
       # Connect to Security & Compliance PowerShell
       if (!$SccSession)
       {
           Import-Module ExchangeOnlineManagement
           Connect-IPPSSession
       }
       # Clean-up, if the script was aborted, the search we created might not have been deleted.  Try to do so now.
       Remove-ComplianceSearch $searchName -Confirm:$false -ErrorAction 'SilentlyContinue'
       # Create a Content Search against the SharePoint Site or OneDrive for Business site and only search for folders; wait for the search to complete
       $complianceSearch = New-ComplianceSearch -Name $searchName -ContentMatchQuery "contenttype:folder OR contentclass:STS_Web" -SharePointLocation $siteUrl
       Start-ComplianceSearch $searchName
       do{
           Write-host "Waiting for search to complete..."
           Start-Sleep -s 5
           $complianceSearch = Get-ComplianceSearch $searchName
       }while ($complianceSearch.Status -ne 'Completed')
       if ($complianceSearch.Items -gt 0)
       {
           # Create a Compliance Search Action and wait for it to complete. The folders will be listed in the .Results parameter
           $complianceSearchAction = New-ComplianceSearchAction -SearchName $searchName -Preview
           do
           {
               Write-host "Waiting for search action to complete..."
               Start-Sleep -s 5
               $complianceSearchAction = Get-ComplianceSearchAction $searchActionName
           }while ($complianceSearchAction.Status -ne 'Completed')
           # Get the results and print out the folders
           $results = $complianceSearchAction.Results
           $matches = Select-String "Data Link:.+[,}]" -Input $results -AllMatches
           foreach ($match in $matches.Matches)
           {
               $rawUrl = $match.Value
               $rawUrl = $rawUrl -replace "Data Link: " -replace "," -replace "}"
               $rawUrls += "DocumentLink:""$rawUrl"""
           }
           $rawUrls |ft
       }
       else
       {
           Write-Host "No folders were found for $siteUrl"
       }
       Remove-ComplianceSearch $searchName -Confirm:$false -ErrorAction 'SilentlyContinue'
    }
    else
    {
       Write-Error "Couldn't recognize $addressOrSite as an email address or a site URL"
    }
    
  2. 在本地计算机上,打开Windows PowerShell并转到保存脚本的文件夹。

  3. 运行脚本;例如:

    .\GetFolderSearchParameters.ps1
    
  4. 输入脚本提示输入的信息。

    该脚本显示指定用户的邮箱文件夹或站点文件夹的列表。 使此窗口保持打开状态,以便可以复制文件夹 ID 或文档链接名称并将其粘贴到步骤 2 中的搜索查询。

    提示

    你可以将脚本的输出重新定向到文本文件,而不是在计算机屏幕上显示文件夹列表。 此文件将保存到脚本所在的文件夹中。 例如,若要将脚本输出重定向到文本文件,请在步骤 3 中运行以下命令: .\GetFolderSearchParameters.ps1 > StacigFolderIds.txt 然后,可以从文件复制文件夹 ID 或文档链接以用于搜索查询。

邮箱文件夹的脚本输出

如果获取邮箱文件夹 ID,脚本将连接到 Exchange Online PowerShell,运行 Get-MailboxFolderStatisics cmdlet,然后显示指定邮箱中的文件夹列表。 对于邮箱中的每个文件夹,脚本在 FolderPath 列中显示文件夹的名称,在 FolderQuery 列中显示文件夹 ID。 此外,该脚本会将 folderId (该前缀是文件夹 ID) 邮箱属性的名称。 由于 folderid 属性是可搜索的属性,因此你将在步骤 2 中的搜索查询中使用 folderid:<folderid> 来搜索该文件夹。

重要

本文中的脚本包含编码逻辑,该逻辑将 Get-MailboxFolderStatistics 返回的 64 个字符的文件夹 ID 值转换为为搜索编制索引的相同 48 个字符格式。 如果只是在 PowerShell 中运行 Get-MailboxFolderStatistics cmdlet 来获取文件夹 ID (而不是运行本文) 中的脚本,则使用该文件夹 ID 值的搜索查询将失败。 必须运行脚本才能获取可在内容搜索中使用的正确格式的文件夹 ID。

下面是邮箱文件夹的脚本返回的输出示例。

脚本返回的邮箱文件夹和文件夹 ID 列表的示例。

步骤 2 中的示例显示了用于在用户的“可恢复项目”文件夹中搜索“清除”子文件夹的查询。

网站文件夹的脚本输出

如果要从 SharePoint 或OneDrive for Business网站获取 documentlink 属性的路径,该脚本将连接到 Security & Compliance PowerShell,创建一个新的内容搜索,用于搜索网站中的文件夹,然后显示位于指定网站中的文件夹列表。 该脚本显示每个文件夹的名称,并将 documentlink 的前缀添加到文件夹 URL。 由于 documentlink 属性是可搜索的属性,因此你将在步骤 2 中的搜索查询中使用 documentlink:<path> property:value 对来搜索该文件夹。 该脚本最多显示 100 个网站文件夹。 如果站点文件夹超过 100 个,则会显示最新的网站文件夹。

下面是站点文件夹的脚本返回的输出示例。

脚本返回的网站文件夹的文档链接名称列表示例。

用于从多个邮箱拉取 FolderID 的脚本

如果需要对 Excel 中的 FolderID 转换结果进行分组和排序,请完成以下步骤,以便为单个用户启用查看多个已删除的文件夹:

  1. 创建名为 UsersGatherFolderIDs 的 .csv 文件,其列标题为 UsersSMTP

  2. 在要转换为文件夹查询语法的任何邮箱文件夹的此列的行中输入用户电子邮件地址。

  3. 使用 .ps1 的文件名后缀将以下文本保存到Windows PowerShell脚本文件;例如,GetMultiUserFolderIDseDiscovery.ps1

     #########################################################################################################
     #This Sample Code is provided for the purpose of illustration only and is not intended to be used in a production environment. 
     #THIS SAMPLE CODE AND ANY RELATED INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS OR A PARTICULAR PURPOSE. 
     #We grant You a nonexclusive, royalty-free right to use and modify the Sample Code and to reproduce and distribute the object code form of the Sample Code, provided that You agree: 
     # (1) to not use Our name, logo, or trademarks to market Your software product in which the Sample Code is embedded; 
     # (2) to include a valid copyright notice on Your software product in which the Sample Code is embedded; and 
     # (3) to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or lawsuits, including attorney's fees, that arise or result from the use or distribution of the Sample Code.
     #########################################################################################################
     " "
     write-host "***********************************************"
     write-host "Security & Compliance Center   " -foregroundColor yellow -backgroundcolor darkgreen
     write-host "eDiscovery cases - FolderID report         " -foregroundColor yellow -backgroundcolor darkgreen
     write-host "***********************************************"
     " "
    
     #prompt users to specify a path to store the output files
     $time = get-date -Format dd-MM-yyyy_hh.mm
     $Path = Read-Host 'Enter a folder path to save the report to a .csv file (filename is created automatically).'
     $inputPath = $Path + '\' + 'Users_GatherFolderID.csv'
     $outputpath = $Path + '\' + 'FileID Report' + ' ' + $time + '.csv'
    
     #Imports list of users
     #User List needs column "UserSMTP" with values of each mailbox's SMTP address.
     $users = Import-CSV $inputPath
    
     function add-tofolderidreport {
         Param(
             [string]$UserEmail,
             [String]$FolderName,
             [String]$FolderID,
             [String]$ConvertedFolderQuery
         )
    
         $addRow = New-Object PSObject
         Add-Member -InputObject $addRow -MemberType NoteProperty -Name "User Email" -Value $useremail
         Add-Member -InputObject $addRow -MemberType NoteProperty -Name "Folder Name" -Value $FolderName
         Add-Member -InputObject $addRow -MemberType NoteProperty -Name "Native Folder ID" -Value $FolderID
         Add-Member -InputObject $addRow -MemberType NoteProperty -Name "Converted Folder Query" -Value $ConvertedFolderQuery
    
         $folderIDReport = $addRow | Select-Object "User Email", "Folder Name", "Native Folder ID", "Converted Folder Query"
         $folderIDReport | export-csv -path $outputPath -notypeinfo -append -Encoding ascii
     }
    
     #get information on the cases and pass values to the FolderID report function
     foreach ($u in $users) {
         $userAddress = $u.UserSMTP
         " "
         write-host "Gathering list of Folders for User:" $userAddress -ForegroundColor Yellow -BackgroundColor Black
         " "
         if ($userAddress.IndexOf("@") -ige 0) {
             # List the folder Ids for the target mailbox
             $emailAddress = $userAddress
             # Connect to Exchange Online PowerShell
             $folderQueries = @()
             $folderStatistics = Get-MailboxFolderStatistics $emailAddress -IncludeSoftDeletedRecipients
             foreach ($folderStatistic in $folderStatistics) {
                 $folderId = $folderStatistic.FolderId;
                 $folderPath = $folderStatistic.FolderPath;
                 $encoding = [System.Text.Encoding]::GetEncoding("us-ascii")
                 $nibbler = $encoding.GetBytes("0123456789ABCDEF");
                 $folderIdBytes = [Convert]::FromBase64String($folderId);
                 $indexIdBytes = New-Object byte[] 48;
                 $indexIdIdx = 0;
                 $folderIdBytes | select -skip 23 -First 24 | % { $indexIdBytes[$indexIdIdx++] = $nibbler[$_ -shr 4]; $indexIdBytes[$indexIdIdx++] = $nibbler[$_ -band 0xF] }
                 $folderQuery = "folderid:$($encoding.GetString($indexIdBytes))";
                 $folderStat = New-Object PSObject
                 Add-Member -InputObject $folderStat -MemberType NoteProperty -Name FolderPath -Value $folderPath
                 Add-Member -InputObject $folderStat -MemberType NoteProperty -Name FolderQuery -Value $folderQuery
                 $folderQueries += $folderStat
    
                 #add information to Report
                 add-tofolderidreport -UserEmail $emailAddress -FolderName $folderPath -FolderID $folderId -ConvertedFolderQuery $folderQuery
             }
    
             #Outputs Exchange Folders for Single User
             Write-Host "-----Exchange Folders-----" -ForegroundColor Yellow
             $folderQueries | ft
    
         }
    
     }
    
     #Provides Path of Report
     " "
     Write-Host "----- Report Output Available at:" "$outputpath" " -----" -ForegroundColor Yellow -BackgroundColor Cyan
     " "
    
  4. 在本地计算机上,打开Windows PowerShell并转到保存脚本的文件夹。 运行脚本文件 GetMultiUserFolderIDseDiscovery.ps1

  5. 输入保存 UsersGatherFolderIDs.csv 文件的文件夹路径。

  6. 该脚本显示指定用户的邮箱文件夹或站点文件夹的列表。 它还会在步骤 4 中指定的同一根文件夹中创建报表。

运行脚本以收集特定用户的文件夹 ID 列表或文档链接后,下一步转到合规性门户并创建新的内容搜索以搜索特定文件夹。 如果使用 New-ComplianceSearch cmdlet) ,则将folderid:<folderid>在“内容搜索关键字 (keyword) (”框中配置的搜索查询中使用 或 documentlink:<path> 属性:value 对或 作为 ContentMatchQuery 参数的值。 可以将 或 documentlink 属性与其他搜索参数或搜索条件组合在一起folderid。 如果在查询中仅包括 folderiddocumentlink 属性,则搜索将返回位于指定文件夹中的所有项目。

  1. 转到 https://compliance.microsoft.com 并使用用于在步骤 1 中运行脚本的帐户和凭据登录。

  2. 在合规性门户的左窗格中,选择“ 显示所有>内容搜索”,然后选择“ 新建搜索”。

  3. 在“ 关键字 ”框中,粘贴 folderid:<folderid> 脚本在步骤 1 中返回的 或 documentlink:<path>/* 值。

    例如,以下屏幕截图中的查询将在用户的“可恢复项目”文件夹中的“清除”子文件夹中搜索任何项目, (folderid “清除”子文件夹的 属性值显示在步骤 1) 的屏幕截图中:

    将 folderid 或 documentlink 粘贴到搜索查询的“关键字 (keyword) ”框中。

    重要

    documentlink 搜索需要使用尾随 asterisk '/*'

  4. 在“ 位置”下,选择“ 特定位置 ”,然后选择“ 修改”。

  5. 根据搜索邮箱文件夹还是网站文件夹,执行以下操作之一:

    • “Exchange 电子邮件”旁边,选择“ 选择用户、组或团队”, 然后添加在步骤 1 中运行脚本时指定的邮箱。

    • SharePoint 网站旁边,选择“ 选择网站 ”,然后添加在步骤 1 中运行脚本时指定的相同网站 URL。

  6. 保存要搜索的内容位置后,选择“ 保存 & 运行”,键入内容搜索的名称,然后选择“ 保存 ”以启动目标集合搜索。

目标集合的搜索查询示例

下面是在搜索查询中使用 folderiddocumentlink 属性执行目标集合的一些示例。 占位符用于 folderid:<folderid>documentlink:<path> 以节省空间。

  • 此示例搜索三个不同的邮箱文件夹。 可以使用类似的查询语法来搜索用户的“可恢复项目”文件夹中的隐藏文件夹。

    folderid:<folderid> OR folderid:<folderid> OR folderid:<folderid>
    
  • 本示例在邮箱文件夹中搜索包含确切短语的项目。

    folderid:<folderid> AND "Contoso financial results"
    
  • 本示例搜索网站文件夹 (和) 的任何子文件夹,查找标题中包含字母“NDA”的文档。

    documentlink:"<path>/*" AND filename:nda
    
  • 本示例搜索网站文件夹 (和任何子文件夹) ,以查找在日期范围内更改的文档。

    documentlink:"<path>/*" AND (lastmodifiedtime>=01/01/2017 AND lastmodifiedtime<=01/21/2017)
    

更多信息

使用本文中的脚本执行目标集合时,请记住以下事项。

  • 脚本不会从结果中删除任何文件夹。 因此,结果中列出的某些文件夹可能无法搜索 (或返回零项目) ,因为它们包含系统生成的内容,或者它们仅包含子文件夹,而不是邮箱项目。
  • 此脚本仅返回用户主邮箱的文件夹信息。 它不会返回有关用户存档邮箱中文件夹的信息。 若要返回有关用户存档邮箱中文件夹的信息,可以编辑脚本。 为此,请将行$folderStatistics = Get-MailboxFolderStatistics $emailAddress$folderStatistics = Get-MailboxFolderStatistics $emailAddress -Archive更改为 ,然后保存并运行已编辑的脚本。 此更改将返回用户存档邮箱中文件夹和子文件夹的文件夹 ID。 若要搜索整个存档邮箱,可以将所有文件夹 ID property:value 对与 OR 搜索查询中的运算符连接。
  • 搜索邮箱文件夹时,只会搜索由其 folderid 属性) 标识的指定文件夹 (;不会搜索子文件夹。 若要搜索子文件夹,需要使用要搜索的子文件夹的文件夹 ID。
  • 搜索网站文件夹时,文件夹 (由其 documentlink 属性标识) ,并将搜索所有子文件夹。
  • 导出仅在搜索查询中指定 folderid 属性的搜索结果时,可以选择第一个导出选项“所有项目(不包括具有无法识别格式的项目)都已加密,或者由于其他原因未编制索引。无论其索引状态如何,都将始终导出文件夹中的所有项目,因为始终为文件夹 ID 编制索引。