使用內容搜尋來搜尋信箱與商務用 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 合規性入口網站 試用中樞開始。 瞭解 有關註冊和試用版條款的詳細數據

許可權和腳本資訊

  • 您必須是合規性入口網站中電子檔探索管理員角色群組的成員,以及 SharePoint Online 全域管理員,才能在步驟 3 中執行腳本。

  • 請務必將您在步驟 2 中建立的使用者清單和步驟 3 中的腳本儲存到相同的資料夾。 這可讓您更輕鬆地執行腳本。

  • 腳本包含最少的錯誤處理。 其主要目的是快速且輕鬆地搜尋每個使用者的信箱和 商務用 OneDrive 網站。

  • 本主題中提供的範例腳本不支援任何 Microsoft 標準支援計劃或服務。 範例指令碼係依「現狀」提供,不含任何種類的擔保方式。 Microsoft 另外不承擔任何明示或默示的擔保,包括但不限於適售性或適合某特定用途的默示擔保。 使用或操作範例指令碼和文件發生的所有風險,皆屬於您的責任。 Microsoft、其作者以及其他與建置、生產或交付程式碼相關的任何人在任何情況下皆完全不需對任何損失負責任,包括但不限於商業利潤損失、業務中斷、業務資訊損失、或其他錢財損失等因使用或無法使用範例指令碼或文件所發生的損失,即使 Microsoft 曾建議這些損失發生的可能性。

步驟 1:安裝 SharePoint Online 管理命令介面

第一個步驟是安裝 SharePoint Online 管理命令介面。 您不需要在此程式中使用殼層,但必須安裝它,因為它包含您在步驟 3 中執行的腳本所需的必要條件。 這些必要條件可讓腳本與 SharePoint Online 通訊,以取得 商務用 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 Online 以取得 商務用 OneDrive URL,以及聯機到安全性 & 合規性 PowerShell。

  • MySite 網域的名稱 - MySite 網域是包含組織中所有 商務用 OneDrive 網站的網域。 例如,如果 MySite 網域的網址的網址是 https://contoso-my.sharepoint.com,則您會在文稿提示您輸入 MySite 網域的名稱時輸入 contoso

  • 步驟 2 中文本檔的路徑名稱 - 您在步驟 2 中建立之文字檔的路徑名稱。 如果文字檔和文稿位於相同的資料夾中,請輸入文字文件的名稱。 否則,請輸入文字檔的完整路徑名稱。

  • 內容搜尋的名稱 - 文稿將建立的內容搜尋名稱。

  • 搜尋查詢 - 將會與內容搜尋搭配使用的搜尋查詢會建立並執行。 如需搜尋查詢的詳細資訊,請參閱 eDiscovery 的關鍵詞查詢和搜尋條件

若要執行文稿:

  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 for Business 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 for Business
    $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 for Business 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,然後建立並開始搜尋。 您可以在安全性 & 合規性 PowerShell 中執行 Get-ComplianceSearch Cmdlet 來顯示搜尋統計數據和結果,也可以移至合規性入口網站中的 [ 內容搜尋 ] 頁面來檢視搜尋的相關信息。