Office 365/Exchange Online 合规性搜索错误 CS007

问题描述 投票:0回答:1

我希望你能帮忙,因为这让我发疯,我想我一定是地球上唯一想要这样做的人......

我想从特定文件夹中删除电子邮件...

错误

“搜索完成 0 项 (0.00 B) 3,120 个未索引项目,3.61 GB 4个邮箱 2 个错误”

“对以下位置的搜索失败: [邮箱]:尝试搜索邮箱时发生暂时错误。请确保您正在搜索的邮箱仍然存在,然后再次运行搜索。 (CS007)”

我正在使用的代码;

Get-PSSession | Remove-PSSession 
Disconnect-ExchangeOnline -Confirm:$false
cls

$user= Get-Content "user.txt"
$passplain= Get-Content "O365Pass.txt"
$pass = ConvertTo-SecureString -String $passplain -AsPlainText -Force

$cred=New-Object System.Management.Automation.PSCredential ($user, $pass)

#$user = "mysupersecretemailadminaddress"      
#pass = 'my super secret super admin password'
[Security.Principal.WindowsIdentity]::GetCurrent()

write-host $user
write-host $pass

#  The section below creates a remote PowerShell connection to Office 365 
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;

Function GetFolderID {

    Param (
        [parameter(Mandatory = $true)]
                [String]
                $Mailbox,
        [parameter(Mandatory = $true)]
                [String]
                $FolderName
        )


    $TargetMailFolders = Get-MailboxFolderStatistics -Identity $Mailbox | Select FolderID, Name

    Try {

        foreach ($TargetMailFolder in $TargetMailFolders) {
    
            $TargetMailFolderID = $TargetMailFolder.FolderID
            $TargetMailFolderName = $TargetMailFolder.Name

            Write-Host "TargetEmailFolderID is $($TargetMailFolderID)"

            If ($TargetMailFolderID) 
                {
                    if ($TargetMailFolderName -eq $FolderName)
                        
                        {
                      
                           Write-Host "TargetEmailFolderID is $($TargetMailFolderID)"
                           Write-Host "TargetEmailFolderID is $($TargetMailFolderName)"
                           Return $TargetMailFolderID
                        }
                }
            Else
                {
                    Write-Host "Target Email Folder does not exist"
                }
        }
    }
    catch {
        throw "There was an error getting email folders"
        break
        }
}

function CleanupMailbox {

    Param(
        [parameter(Mandatory = $true)]
        [String]
        $Mailbox,
        #$mailboxes = @("[email protected]", "[email protected]")
        [parameter(Mandatory = $true)]
        [String]
        $DaysToKeep,
        #$DaysToKeep = 3
        [parameter(Mandatory = $true)]
        [String]
        $SearchName,
        [parameter(Mandatory = $true)]
        [String]
        $SearchQuery

    )

    # Clean-up any old searches from failed runs of this script
    if (Get-ComplianceSearch -Identity $searchName) {
        Write-Host "Cleaning up any old searches from failed runs of this script"

        try {
            Remove-ComplianceSearch -Identity $searchName -Confirm:$false | Out-Null
            }
        catch {
            Write-Host "Clean-up of old script runs failed!" -ForegroundColor Red
            break
            }
        }

    Write-Host "Creating new search for emails older than $($DateSearch)"

    
                       
                            New-ComplianceSearch -Name $searchName -ExchangeLocation $Mailbox -ContentMatchQuery $SearchQuery -AllowNotFoundExchangeLocationsEnabled $true | Out-Null
                            
                                                                            
                            Start-ComplianceSearch -Identity $searchName | Out-Null

                            Write-Host "Searching..." -NoNewline

                            while ((Get-ComplianceSearch -Identity $searchName).Status -ne "Completed") {
                                Write-Host "." -NoNewline
                                Start-Sleep -Seconds 2
                            }

                            $items = (Get-ComplianceSearch -Identity $searchName).Items

                            if ($items -gt 0) {
                                $searchStatistics = Get-ComplianceSearch -Identity $searchName | Select-Object -Expand SearchStatistics | Convertfrom-JSON

                                $sources = $searchStatistics.ExchangeBinding.Sources | Where-Object { $_.ContentItems -gt 0 }

                                Write-Host ""
                                Write-Host "Total Items found matching query:" $items 
                                Write-Host ""
                                Write-Host "Items found in the following mailboxes"
                                Write-Host "--------------------------------------"

                                foreach ($source in $sources) {
                                    Write-Host $source.Name "has" $source.ContentItems "items of size" $source.ContentSize
                                }

                                Write-Host ""

                                $iterations = 0;
    
                                $itemsProcessed = 0
    
                                while ($itemsProcessed -lt $items) {
                                    $iterations++

                                    Write-Host "Deleting items iteration $($iterations)"

                                    #########################the below line does the delete
                                    New-ComplianceSearchAction -SearchName $searchName -Purge -PurgeType SoftDelete -Confirm:$false | Out-Null

                                    while ((Get-ComplianceSearchAction -Identity "$($searchName)_Purge").Status -ne "Completed") { 
                                        Start-Sleep -Seconds 2
                                    }

                                    $itemsProcessed = $itemsProcessed + 10
        
                                    # Remove the search action so we can recreate it
                                    Remove-ComplianceSearchAction -Identity "$($searchName)_Purge" -Confirm:$false  
                                }
                            } else {
                                Write-Host "No items found"
                            }

                            Write-Host ""
                            Write-Host "COMPLETED!"

                        }
 


Import-Module ExchangeOnlineManagement     
Connect-ExchangeOnline -Credential $cred
Connect-IPPSSession -Credential $cred

Write-Host "$(Get-Date -Format u) Cleanup $($Mailbox) Started"

$Mailbox = "emailaddresshere"
$FolderName = "foldernamehere"

$FolderID=GetFolderID -Mailbox $Mailbox -FolderName $FolderName
write-host "Returned Folder ID is: $($FolderID)"

$SearchName  = "Cleanup$($Mailbox)$($Foldername)"
$DaysToKeep  = 90
$DateSearch=(Get-Date).AddDays(-$($DaysToKeep)).ToString("yyyy-MM-dd") #ToString("MM/dd/yyyy")
write-host "$(Get-Date -Format u) DateSearch is $DateSearch"

$SearchQuery = "(subject:'subjectstringhere') AND (folderid:$($FolderID))"

write-host "Search query is <$($SearchQuery)>"

CleanupMailbox -Mailbox $Mailbox -DaysToKeep $DaysToKeep -SearchName $SearchName -SearchQuery $SearchQuery

Write-Host "$(Get-Date -Format u) Cleanup $($Mailbox) Finished"

Get-PSSession | Remove-PSSession 
Write-Host "Cleanup complete"

所以我的脚本获取文件夹的folderid(这是正确的)并运行。

然后我什么也没发生。

当我在门户网站上检查合规性时,我的搜索结果出现上述错误。

我只处理一个邮箱......而不是 4 个。

如果我在没有folderid的情况下编辑并运行搜索,它就可以工作。

如果我使用查询生成器构建搜索,尽管有文档,它不会从下拉列表中为我提供“folderid”,但会正确执行“错误检查”folderid。

我使用哪个收件箱、哪个文件夹并不重要。

如果我省略folderid,它会运行,如果我包含它,则无论邮箱或文件夹如何,它都会失败并显示以下消息。

我已经尝试了“或'、=或:的所有组合。

那么文档是垃圾还是我遗漏了一些明显的东西?

我使用了错误的 cmdlet 吗?

powershell exchange-server exchangewebservices o365security-compliance
1个回答
0
投票

folderid
中的
Get-MailboxFolderStatistics
需要重新格式化才能与关键字查询语言 (KQL) 一起使用。

这段代码可以做到这一点:

$folderStatistics = Get-MailboxFolderStatistics $Mailbox
$folderQueries = 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-Object -Skip 23 -First 24 | ForEach-Object { $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 -PassThru
}
$folderQueries | Sort-Object -Property FolderPath
© www.soinside.com 2019 - 2024. All rights reserved.