获取特定 public 文件夹的所有子文件夹

Get all subfolders of a specific public folder

我想获取特定 public 子文件夹的所有子文件夹。

我面临的问题是“public 文件夹不允许深度遍历查询。”所以我现在有点不知道该怎么做。

这是我的代码:

Function FindAllSubfolders(){
    $tfTargetidRoot = new-object  Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::PublicFoldersRoot)
    $tfTargetFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$tfTargetidRoot)

    $FolderPath = "/mydomain.com/parentfolder"
    $pfArray = $FolderPath.Split("/")
    for ($lint = 1; $lint -lt $pfArray.Length; $lint++) {
        $pfArray[$lint]
        $fvFolderView = new-object Microsoft.Exchange.WebServices.Data.FolderView(1)
        $SfSearchFilter = new-object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo([Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName,$pfArray[$lint])
        $findFolderResults = $service.FindFolders($tfTargetFolder.Id,$SfSearchFilter,$fvFolderView)
        if ($findFolderResults.TotalCount -gt 0){
            foreach($folder in $findFolderResults.Folders){
                $tfTargetFolder = $folder               
            }
        }
    }
    
    $folderview = New-Object Microsoft.Exchange.WebServices.Data.FolderView(10000)
    $folderview.PropertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.Webservices.Data.BasePropertySet]::FirstClassProperties)
    $folderview.PropertySet.Add([Microsoft.Exchange.Webservices.Data.FolderSchema]::DisplayName)
    $folderview.Traversal = [Microsoft.Exchange.Webservices.Data.FolderTraversal]::Deep
    $folderfindResults = $service.FindFolders($tfTargetFolder.id, $folderview)
    return $folderfindResults
}

未经测试,但您可以试试这个:

$result = Get-PublicFolder -Identity "/mydomain.com/parentfolder" -Recurse -ResultSize Unlimited -ErrorAction SilentlyContinue | ForEach-Object {
    [PsCustomObject]@{            
        FolderPath   = $_.Identity
        DisplayName  = $_.DisplayName
        EmailAddress = if ($_.MailEnabled) {$_.PrimarySmtpAddress.ToLower()} else {$null}
    }
}

# show the results
$result | Format-Table -AutoSize  # or Out-GridView if you prefer

# save as CSV file
$result | Export-Csv -Path 'X:\path\to\PublicFolders.csv' -NoTypeInformation

使用 EWS 执行此操作的唯一方法是您需要对每个文件夹进行浅遍历,如果该子文件夹有子文件夹则处理结果,然后对该子文件夹进行浅遍历等等,直到获得整个文件夹树。 Public 文件夹可能会变得复杂,例如,如果您随后想开始访问文件夹的内容,则子文件夹可能位于不同的 Public 文件夹邮箱中。