Get-EWSFolder.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function Get-EWSFolder  {
    [OutputType('Microsoft.Exchange.WebServices.Data.Folder')]
    [CmdletBinding()]
    param (
        [Parameter(
                Mandatory
        )]
        [ValidateScript({
                    $root, $rest = $_ -split '\\|/|:|\|'
                    try { 
                        [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]$root 
                        $true
                    } catch {
                        throw "Root folder must belong to collection of WellKnownFolderName: $_"
                    }
        })]
        [string]$Path,

        [Microsoft.Exchange.WebServices.Data.ExchangeService]$Service = $Script:exchangeService,
        
        [Microsoft.Exchange.WebServices.Data.Mailbox]$Mailbox
    )
    
    if (-not $Service) {
        Write-Warning 'No connection defined. Use Connect-EWSService first!'
        return
    }

    if (-not $PSBoundParameters.ContainsKey('Mailbox')) {
        if (-not ($Mailbox = $connections[$Service])) {
            $Mailbox = $Script:Mailbox
        }
    }
    $name = [Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName
    $rootFolderName, $remaingPath = $Path -split '\\|/|:|\|'
    try {
        $rootFolder = New-Object Microsoft.Exchange.WebServices.Data.FolderId $rootFolderName, $Mailbox
    } catch {
        throw "Unexpected error occurred: $_"
    }
    $root = [Microsoft.Exchange.WebServices.Data.Folder]::Bind(
        $Service,
        $rootFolder
    )

    foreach ($item in $remaingPath) {
        $filter = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo -ArgumentList @(
            $name,
            $item
        )
        $lastItem = 
            New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+ContainsSubstring -ArgumentList @(
                $name,
                $item
            )
        try {
            $new = $root.FindFolders($filter,1).Folders[0]
            if ($new) {
                $root = $new
            } else {
                $view = New-Object Microsoft.Exchange.WebServices.Data.FolderView 10, 0
                do {
                    $list = $root.FindFolders($lastItem,$view)
                    $list
                    $view.Offset += $list.Items.Count
                } while ($list.MoreAvailable)
            }
        } catch {
            Write-Error "Could not find subfolder: $item"
            $root
            return
        }
    }
    $root
}