Query on web services

Glenn Maxwell 11,316 Reputation points
2024-08-03T16:32:10.7233333+00:00

Hi all,

I am using an Exchange 2016 hybrid environment. I had a script that was working fine with Exchange 2010. After upgrading to Exchange 2016, the script stopped working. I have a member server on which I installed the Exchange 2016 management tools. The purpose of this script is to forward Calendar invites to newly joined employees. However, I don't see the below path on the server:

C:\Program Files\Microsoft\Exchange\Web Services\2.2

i see this path on the server (C:\Program Files\Microsoft\Exchange Server\V15)Do I need to install web services on this server? Below is the script. Please guide me on what changes I need to make to run it on Exchange 2016.

Start-Transcript -Path "C:\temp\transcript.txt"
$OU = "contoso.com/OU1"
$CSV = ".\userlist.csv"
$Users = Import-Csv '.\userlist.csv'
$MBX = '[email protected]'
$Items = 50
$DaysInTheFuture = 300
$Now = [System.DateTime]::Now
$Then = [System.DateTime]::Now.AddDays($DaysInTheFuture)
$username = "[email protected]"
$password = "somepwd"
$TenantPass = $password | ConvertTo-SecureString -AsPlainText -Force
$EWSServicePath = 'C:\Program Files\Microsoft\Exchange\Web Services\2.2\Microsoft.Exchange.WebServices.dll'
Import-Module $EWSServicePath
$Service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$ExchVer = [Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1
$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,$MailboxToImpersonate)
$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) | 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
Microsoft Exchange Online
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,514 questions
Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
544 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,634 questions
Microsoft Exchange Hybrid Management
Microsoft Exchange Hybrid Management
Microsoft Exchange: Microsoft messaging and collaboration software.Hybrid Management: Organizing, handling, directing or controlling hybrid deployments.
2,093 questions
0 comments No comments
{count} votes

Accepted answer
  1. Mike Hu-MSFT 3,675 Reputation points Microsoft Vendor
    2024-08-05T09:32:53.1066667+00:00

    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:

    1. 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.
    2. 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.
    3. Update the Exchange version in the script: Since you're using Exchange 2016, update the ExchangeVersion in the script to Exchange2016.
    4. 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 = '[email protected]'
    $Items = 50
    $DaysInTheFuture = 300
    $Now = [System.DateTime]::Now
    $Then = [System.DateTime]::Now.AddDays($DaysInTheFuture)
    $username = "[email protected]"
    $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
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.