使用内容搜索在邮箱和 OneDrive 网站中搜索用户列表

安全性 & 合规性 PowerShell 提供了许多 cmdlet,使你能够自动执行耗时的电子数据展示相关任务。 目前,在 Microsoft Purview 合规性门户中创建内容搜索以搜索大量保管人内容位置需要时间和准备。 在创建搜索之前,必须收集每个 OneDrive 网站的 URL,然后将每个邮箱和 OneDrive 网站添加到搜索。 在将来的版本中,这在合规性门户中更容易实现。 在此之前,可以使用本文中的脚本自动执行此过程。 此脚本会提示输入组织的 MySite 域的名称 (例如,URL https://contoso-my.sharepoint.com) 中的 contoso、用户电子邮件地址列表、新内容搜索的名称以及要使用的搜索查询。 该脚本获取列表中每个用户的 OneDrive URL,然后使用提供的搜索查询创建并启动内容搜索,用于搜索列表中的每个用户的邮箱和 OneDrive 网站。

提示

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

权限和脚本信息

重要

Microsoft建议使用权限最少的角色。 最大程度地减少具有全局管理员角色的用户数,有助于提高组织的安全性。 详细了解 Microsoft Purview 角色和权限

  • 您必须是 Microsoft Purview 门户或合规性门户中电子数据展示管理员角色组的成员,并且必须是 SharePoint 全局管理员才能在步骤 3 中运行脚本。
  • 请务必将步骤 2 中创建的用户列表和步骤 3 中的脚本保存到同一文件夹。 这样可以更轻松地运行脚本。
  • 该脚本包含最少的错误处理。 其主要用途是快速轻松地搜索每个用户的邮箱和 OneDrive 站点。
  • 本文中提供的示例脚本在任意 Microsoft 标准支持程序或服务下都不受支持。 示例脚本“原样”提供,不提供任何形式的保证。 Microsoft 进一步拒绝所有默示保证,包括但不限于针对特定用途的适销性或适用性的任何默示保证。 由于示例脚本及文档的使用或性能所引起的全部风险均由您承担。 在任何情况下,对于由于使用或者无法使用示例脚本或文档所引起的任何损失(包括但不限于商业利润损失、业务中断、商业信息丢失或者其他经济损失),Microsoft、其作者或者参与创建、制作或交付脚本的任何人概不负责,即使 Microsoft 已被告知可能会出现此类损失。

步骤 1:安装 SharePoint Online 命令行管理程序

第一步是安装 SharePoint Online 命令行管理程序。 在此过程中,无需使用 shell,但必须安装它,因为它包含步骤 3 中运行的脚本所需的先决条件。 这些先决条件允许脚本与 SharePoint 通信以获取 OneDrive 网站的 URL。

转到 设置 SharePoint Online 命令行管理程序环境 ,并执行步骤 1 和步骤 2 以安装 SharePoint Online 命令行管理程序。

步骤 2:生成用户列表

步骤 3 中的脚本将创建内容搜索,以搜索邮箱和 OneDrive 帐户以获取用户列表。 只需在文本文件中键入电子邮件地址,也可以在 PowerShell 中运行命令来获取电子邮件地址列表,并将其保存到位于步骤 3) 中将脚本保存到的同一文件夹中的文件中 (。

下面是一个 Exchange Online PowerShell 命令,可以运行该命令来获取组织中所有用户的电子邮件地址列表,并将其保存到名为 的 Users.txt文本文件中。

Get-Mailbox -ResultSize unlimited -Filter { RecipientTypeDetails -eq 'UserMailbox'} | Select-Object PrimarySmtpAddress > Users.txt

运行此命令后,请确保打开 文件并删除包含属性名称 PrimarySmtpAddress的标头。 文本文件应仅包含电子邮件地址列表,而不包含其他任何内容。 确保电子邮件地址列表之前或之后没有空白行。

在此步骤中运行脚本时,系统会提示输入以下信息。 在运行脚本之前,请务必准备好此信息。

  • 用户凭据 - 脚本使用凭据访问 SharePoint 以获取 OneDrive URL,并连接到安全性 & 合规性 PowerShell。

  • MySite 域的名称 - MySite 域是包含组织中所有 OneDrive 网站的域。 例如,如果 MySite 域的 URL 为 https://contoso-my.sharepoint.com,则当脚本提示输入 MySite 域的名称时,可以输入 contoso

  • 步骤 2 中文本文件 的路径名 - 在步骤 2 中创建的文本文件的路径名。 如果文本文件和脚本位于同一文件夹中,则输入文本文件的名称。 否则,请输入文本文件的完整路径名。

  • 内容搜索的名称 - 脚本将创建的内容搜索的名称。

  • 搜索查询 - 创建并运行与内容搜索一起使用的搜索查询。 有关搜索查询的详细信息,请参阅 电子数据展示的关键字查询和搜索条件

若要运行该脚本,请执行下列操作:

  1. 使用文件名后缀 .ps1 将以下文本保存到 Windows PowerShell 脚本文件;例如 。 SearchEXOOD4B.ps1 将文件保存到步骤 2 中保存用户列表的同一文件夹。

    # This PowerShell script will prompt you for the following information:
    #    * Your user credentials
    #    * The name of your organization's MySite domain
    #    * The pathname for the text file that contains a list of user email addresses
    #    * The name of the Content Search that will be created
    #    * The search query string
    # The script will then:
    #    * Find the OneDrive site for each user in the text file
    #    * Create and start a Content Search using the above information
    # Get user credentials
    if (!$credentials)
    {
        $credentials = Get-Credential
    }
    # Get the user's MySite domain name.  We use this to create the admin URL and root URL for OneDrive
    $mySiteDomain = Read-Host "What is your organization's MySite domain?  For example,  'contoso' for 'https://contoso-my.sharepoint.com'"
    $AdminUrl = "https://$mySiteDomain-admin.sharepoint.com"
    $mySiteUrlRoot = "https://$mySiteDomain-my.sharepoint.com"
    # Get other required information
    $inputfile = read-host "Enter the file name of the text file that contains the email addresses for the users you want to search"
    $searchName = Read-Host "Enter the name for the new search"
    $searchQuery = Read-Host "Enter the search query you want to use"
    $emailAddresses = Get-Content $inputfile | where {$_ -ne ""}  | foreach{ $_.Trim() }
    # Connect to Security & Compliance PowerShell
    if (!$s -or !$a)
    {
        Import-Module ExchangeOnlineManagement
        Connect-IPPSSession
    }
    
    # Load the SharePoint assemblies from the SharePoint Online Management Shell
    # To install, go to https://go.microsoft.com/fwlink/p/?LinkId=255251
    if (!$SharePointClient -or !$SPRuntime -or !$SPUserProfile)
    {
        $SharePointClient = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
        $SPRuntime = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
        $SPUserProfile = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles")
        if (!$SharePointClient)
        {
            Write-Error "SharePoint Online Management Shell isn't installed, please install from: https://go.microsoft.com/fwlink/p/?LinkId=255251 and then run this script again"
            return;
        }
    }
    if (!$spCreds)
    {
        $spCreds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($credentials.UserName, $credentials.Password)
    }
    # Add the path of the User Profile Service to the SPO admin URL, then create a new webservice proxy to access it
    $proxyaddr = "$AdminUrl/_vti_bin/UserProfileService.asmx?wsdl"
    $UserProfileService= New-WebServiceProxy -Uri $proxyaddr -UseDefaultCredential False
    $UserProfileService.Credentials = $credentials
    # Take care of auth cookies
    $strAuthCookie = $spCreds.GetAuthenticationCookie($AdminUrl)
    $uri = New-Object System.Uri($AdminUrl)
    $container = New-Object System.Net.CookieContainer
    $container.SetCookies($uri, $strAuthCookie)
    $UserProfileService.CookieContainer = $container
    Write-Host "Getting each user's OneDrive URL"
    $urls = @()
    foreach($emailAddress in $emailAddresses)
    {
        try
        {
            $prop = $UserProfileService.GetUserProfileByName("i:0#.f|membership|$emailAddress") | Where-Object { $_.Name -eq "PersonalSpace" }
            $url = $prop.values[0].value
            $furl = $mySiteUrlRoot + $url
            $urls += $furl
            Write-Host "-$emailAddress => $furl"
        }
        catch
        {
            Write-Warning "Could not locate OneDrive for $emailAddress"
        }
    }
    Write-Host "Creating and starting the search"
    $search = New-ComplianceSearch -Name $searchName -ExchangeLocation $emailAddresses -SharePointLocation $urls -ContentMatchQuery $searchQuery
    # Finally, start the search and then display the status
    if($search)
    {
        Start-ComplianceSearch $search.Name
        Get-ComplianceSearch $search.Name
    }
    
  2. 打开 Windows PowerShell,转到保存脚本的文件夹和步骤 2 中的用户列表。

  3. 启动脚本;例如:

    .\SearchEXOOD4B.ps1
    
  4. 系统提示输入凭据时,请输入电子邮件地址和密码,然后选择“ 确定”。

  5. 在脚本提示时输入以下信息。 键入每条信息,然后按 Enter

    • MySite 域的名称。

    • 包含用户列表的文本文件的路径名。

    • 内容搜索的名称。

    • 搜索查询 (保留此空白,以返回内容位置) 中的所有项。

    该脚本获取每个 OneDrive 网站的 URL,然后创建并启动搜索。 可以在安全性 & Compliance PowerShell 中运行 Get-ComplianceSearch cmdlet 以显示搜索统计信息和结果,也可以转到合规性门户中 的内容搜索 页以查看有关搜索的信息。