New-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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
function New-EWSFolder {
    [OutputType('Microsoft.Exchange.WebServices.Data.Folder')]
    [CmdletBinding(
            DefaultParameterSetName = 'byName'
    )]
    param (
        [Parameter(
                Mandatory,
                ValueFromPipelineByPropertyName
        )]
        [string]$DisplayName,

        [Parameter(
                ValueFromPipelineByPropertyName
        )]
        [ValidateSet(
                'Calendar',
                'Contacts',
                'Tasks',
                'Mail'
        )]
        [string]$FolderType = 'Mail',

        [Parameter(
                ParameterSetName = 'byId',
                ValueFromPipelineByPropertyName,
                Mandatory
        )]
        [Alias('Id')]
        [Microsoft.Exchange.WebServices.Data.FolderId]$ParentId,

        [Parameter(
                ParameterSetName = 'byName',
                Mandatory
        )]
        [Microsoft.Exchange.WebServices.Data.WellKnownFolderName]$ParentName,

        [Parameter(
                ValueFromPipelineByPropertyName
        )]
        [Microsoft.Exchange.WebServices.Data.ExchangeService]$Service = $Script:exchangeService,
        
        [Microsoft.Exchange.WebServices.Data.Mailbox]$Mailbox

    )

    begin {
        $type = 'Microsoft.Exchange.WebServices.Data.{0}Folder' -f "$(
            if ($FolderType -ne 'Mail') {
            $FolderType
            }
        )"

    }

    process {
        if (-not $Service) {
            return
        }

        if ($PSCmdlet.ParameterSetName -eq 'byName') {
            if (-not $PSBoundParameters.ContainsKey('Mailbox')) {
                if (-not ($Mailbox = $Connections[$Service])) {
                    $Mailbox = $Script:Mailbox
                }
            }

            $folder = New-Object Microsoft.Exchange.WebServices.Data.FolderId $ParentName, $Mailbox
            $parentId = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($Service, $ParentName).Id
        }

        $newFolder = New-Object $Type $Service
        if ($FolderType -eq 'Mail') {
            $newFolder.FolderClass = 'IPF.Note'
        }
        $newFolder.DisplayName = $DisplayName
        $newFolder.Save($parentId)
    
        $name = [Microsoft.Exchange.WebServices.Data.FolderSchema]::DisplayName
        $root = [Microsoft.Exchange.WebServices.Data.Folder]::Bind(
            $Service,
            $parentId
        )

        $filter = New-Object Microsoft.Exchange.WebServices.Data.SearchFilter+IsEqualTo -ArgumentList @(
            $name,
            $DisplayName
        )
        try {
            $root.FindFolders($filter,1).Folders[0]
        } catch {}
    }
    end {
        if (-not $Service) {
            Write-Warning 'No connection defined. Use Connect-EWSService first!'
            return
        }
    }
}