New-EWSAppointment.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
100
101
102
103
104
105
106
107
function New-EWSAppointment {
    [OutputType('Microsoft.Exchange.WebServices.Data.Appointment')]
    [CmdletBinding(
            DefaultParameterSetName = 'inline'
    )]
    param (
        [string[]]$Required,

        [string[]]$Optional,

        [Parameter(
                Mandatory
        )]
        [string]$Subject,

        [Parameter(
                Mandatory,
                ParameterSetName = 'inline'
        )]
        [string]$Body,

        [Microsoft.Exchange.WebServices.Data.BodyType]$BodyType = 'Text',

        [string]$Location,

        [Parameter(
                Mandatory
        )]
        [DateTime]$Start,

        [Parameter(
                Mandatory
        )]
        [TimeSpan]$Duration,

        [string[]]$Attachment,
        [Parameter(
                ValueFromPipeline,
                Mandatory,
                ParameterSetName = 'pipe'
        )]
        $InputObject,

        [Parameter(
                ParameterSetName = 'pipe'
        )]
        [switch]$IsHtml,
        
        [Parameter(
                ValueFromPipelineByPropertyName
        )]
        [Microsoft.Exchange.WebServices.Data.ExchangeService]$Service = $script:exchangeService
    )

    begin {
        $data = New-Object Collections.ArrayList
    }

    process {
        if ($InputObject) {
            $null = $data.Add($InputObject)
        }
    }

    end {
        if (-not $Service) {
            Write-Warning 'No connection defined. Use Connect-EWSService first!'
            return
        }
        if ($data.Count) {
            if (-not $IsHtml -and $BodyType -eq 'HTML') {
                $body = $data | ConvertTo-Html | Out-String
            } else {
                $body = $data | Out-String
            }
        }
        $appointment = New-Object Microsoft.Exchange.WebServices.Data.Appointment $Service -Property @{
            Subject = $Subject
            Body = $body
            Start = $Start
            End = $Start + $Duration
            Location = $Location
        }
        $appointment.Body.BodyType = $BodyType
        foreach ($file in $Attachment) {
            $null = $appointment.Attachments.AddFileAttachment($file)
        }
        foreach ($attendee in $Required) {
            $null = $appointment.RequiredAttendees.Add(
                $attendee
            )
        }
        foreach ($attendee in $Optional) {
            $null = $appointment.OptionalAttendees.Add(
                $attendee
            )
        }
        if ($Required -or $Optional) {
            $method = [Microsoft.Exchange.WebServices.Data.SendInvitationsMode]::SendToAllAndSaveCopy
        } else {
            $method = [Microsoft.Exchange.WebServices.Data.SendInvitationsMode]::SendToNone
        }
        $appointment.Save($method)
        $appointment
    }
}