Looks like your issue is more related to development, please kindly understand that the Exchange tag here we mainly focus on general issues about Exchange. In order to better solve your problem, we will add "Exchange Development" tag.
But based on my personal experience, you can do the following:
- Install the EWS Managed API: Since you are missing the
C:\Program Files\Microsoft\Exchange\Web Services\2.2\
path, you need to download and install the EWS Managed API if it isn't already installed. You can refer to: https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/get-started-with-ews-managed-api-client-applications. After downloading, install it to get the DLL file. - Update the path to the EWS DLL: Adjust your script to reflect the correct location of the EWS Managed API DLL file. The default installation path is usually
C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll
. - Update the Exchange version in the script: Since you're using Exchange 2016, update the
ExchangeVersion
in the script toExchange2016
. - Ensure proper permissions: Ensure that the account you're using has the necessary permissions to impersonate the mailbox and access the calendar.
Below is the modified script with these changes implemented:
Start-Transcript -Path "C:\temp\transcript.txt"
$OU = "contoso.com/OU1"
$CSV = ".\userlist.csv"
$Users = Import-Csv '.\userlist.csv'
$MBX = 'sharedmailbox@contoso.com'
$Items = 50
$DaysInTheFuture = 300
$Now = [System.DateTime]::Now
$Then = [System.DateTime]::Now.AddDays($DaysInTheFuture)
$username = "admin@contoso.com"
$password = "somepwd"
$TenantPass = $password | ConvertTo-SecureString -AsPlainText -Force
# Correct path to the EWS Managed API DLL
$EWSServicePath = 'C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll'
Import-Module $EWSServicePath
# Creating the Exchange service object and setting the version to Exchange 2016
$ExchVer = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2016
$Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService($exchver)
$Credentials = New-Object System.Net.NetworkCredential($username, $password)
$service.Credentials = $Credentials
# Setting up EWS URL
$EWSurl = "https://outlook.office365.com/EWS/Exchange.asmx"
$Service.URL = $EWSurl
$service.ImpersonatedUserId = New-Object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId `
([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SMTPAddress,$MBX);
$folderid = new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar,$MBX)
$calendarFolder = [Microsoft.Exchange.WebServices.Data.CalendarFolder]::Bind($service,$folderid)
$calendarView = new-object Microsoft.Exchange.WebServices.Data.CalendarView($Now, $Then)
$calendarView.MaxItemsReturned = $Items
$calendarView.PropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$findItemResults = $calendarFolder.FindAppointments($calendarView)
if ($findItemResults) {
$FutureMeetings = @()
Foreach ($CalItem in $findItemResults) {
If ($CalItem.IsMeeting -eq $True) {
$FutureMeetings += $CalItem
}
}
if ($FutureMeetings) {
$emaillist = @()
foreach ($user in $Users) {
$EmpType = $null
$EmpType = get-qaduser $user.UserPrincipalName -properties employeeType | select employeeType
If (($user.RecipientTypeDetails -eq 'UserMailbox')) {
$emaillist += $user.emailaddress
}
}
if ($emaillist.count -gt 0) {
foreach ($item in $FutureMeetings) {
[void]$item.Forward("shared meeting invite with you.",$emaillist)
("Date: "+$Now) | out-file ".\fwreport.csv" -append
("Addresses: "+$emaillist) | out-file ".\fwreport.csv" -append
("Subject: "+$item.Subject) | out-file ".\fwreport.csv" -append
("Start: "+$item.Start
("Start: "+$item.Start) | out-file ".\fwreport.csv" -append
("End: "+$item.End) | out-file ".\fwreport.csv" -append
("Location: "+$item.Location) | out-file ".\fwreport.csv" -append
("--------------------------------------------------------------") | out-file ".\fwreport.csv" -append
}
}
}
}
Stop-Transcript