xEXOUserAvailability.psm1
<#
The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages. #> #requires -Version 3 #Import Localized Data Import-LocalizedData -BindingVariable Messages $webSvcInstallDirRegKey = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Exchange\Web Services\2.0" -PSProperty "Install Directory" -ErrorAction:SilentlyContinue if ($webSvcInstallDirRegKey -ne $null) { $moduleFilePath = $webSvcInstallDirRegKey.'Install Directory' + 'Microsoft.Exchange.WebServices.dll' Import-Module $moduleFilePath } else { $errorMsg = $Messages.InstallExWebSvcModule throw $errorMsg } Function New-OSCPSCustomErrorRecord { #This function is used to create a PowerShell ErrorRecord Param ( [String]$ExceptionString, [String]$ErrorID, [System.Management.Automation.ErrorCategory]$ErrorCategory, [PSObject]$TargetObject ) Process { $exception = New-Object -TypeName System.Management.Automation.RuntimeException -ArgumentList $ExceptionString $customError = New-Object -TypeName System.Management.Automation.ErrorRecord -ArgumentList $exception,$ErrorID,$ErrorCategory,$TargetObject return $customError } } Function Connect-OSCEXOWebService { #.EXTERNALHELP Connect-OSCEXOWebService-Help.xml [CmdletBinding()] Param ( [Parameter(Mandatory=$true, Position=1)] [System.Management.Automation.PSCredential]$Credential, [Parameter(Mandatory=$false,Position=2)] [Microsoft.Exchange.WebServices.Data.ExchangeVersion]$ExchangeVersion="Exchange2010_SP2", [Parameter(Mandatory=$false,Position=3)] [string]$TimeZoneStandardName, [Parameter(Mandatory=$false)] [switch]$Force ) Process { # Get specific time zone info if (-not [System.String]::IsNullOrEmpty($TimeZoneStandardName)) { try { $tzInfo = [System.TimeZoneInfo]::FindSystemTimeZoneById($TimeZoneStandardName) } catch { $PSCmdlet.ThrowTerminatingError($_) } } else { $tzInfo = $null } # Create the callback to validate the redirection URL. $validateRedirectionUrlCallback = { param ([string]$Url) if ($Url -eq "https://autodiscover-s.outlook.com/autodiscover/autodiscover.xml") { return $true } else { return $false } } # Try to get exchange service object from global scope $existingExSvcVar = (Get-Variable -Name exService -Scope Global -ErrorAction:SilentlyContinue) -ne $null # Establish the connection to Exchange Web Service if ((-not $existingExSvcVar) -or $Force) { $verboseMsg = $Messages.EstablishConnection $PSCmdlet.WriteVerbose($verboseMsg) if ($tzInfo -ne $null) { $exService = New-Object -TypeName Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList $ExchangeVersion, $tzInfo } else { $exService = New-Object -TypeName Microsoft.Exchange.WebServices.Data.ExchangeService -ArgumentList $ExchangeVersion } # Set network credential $userName = $Credential.UserName $password = $Credential.GetNetworkCredential().Password $exService.Credentials = New-Object -TypeName System.Net.NetworkCredential -ArgumentList $userName,$password try { #Set the URL by using Autodiscover $exService.AutodiscoverUrl($userName,$validateRedirectionUrlCallback) $verboseMsg = $Messages.SaveExWebSvcVariable $PSCmdlet.WriteVerbose($verboseMsg) Set-Variable -Name exService -Value $exService -Scope Global -Force } catch [Microsoft.Exchange.WebServices.Autodiscover.AutodiscoverRemoteException] { $PSCmdlet.ThrowTerminatingError($PSItem) } catch { $PSCmdlet.ThrowTerminatingError($PSItem) } } else { $verboseMsg = $Messages.FindExWebSvcVariable $verboseMsg = $verboseMsg -f $exService.Credentials.Credentials.UserName $PSCmdlet.WriteVerbose($verboseMsg) } } } Function Get-OSCEXOUserAvailability { #.EXTERNALHELP Get-OSCEXOUserAvailability-Help.xml [CmdletBinding()] Param ( [Parameter(Mandatory=$true,Position=1,ValueFromPipeline=$true)] [string]$Identity, [Parameter(Mandatory=$false,Position=2)] [datetime]$StartDate=(Get-Date), [Parameter(Mandatory=$false,Position=3)] [datetime]$EndDate=(Get-Date).AddDays(1) ) Begin { #Verify the existence of exchange web service if ($exService -eq $null) { $errorMsg = $Messages.RequireConnection $customError = New-OSCPSCustomErrorRecord -ExceptionString $errorMsg -ErrorCategory NotSpecified -ErrorID 1 -TargetObject $PSCmdlet $PSCmdlet.ThrowTerminatingError($customError) } } Process { $verboseMsg = $Messages.ResolveIdentity $verboseMsg = $verboseMsg -f $Identity $PSCmdlet.WriteVerbose($verboseMsg) # Resolve Identity parameter to get user's SMTP address $nameResolutionCollection = $exService.ResolveName($Identity, [Microsoft.Exchange.WebServices.Data.ResolveNameSearchLocation]::DirectoryOnly, $true) if ($nameResolutionCollection.Count -ne 1) { $errorMsg = $Messages.InvalidIdentity $errorMsg = $errorMsg -f $Identity $customError = New-OSCPSCustomErrorRecord -ExceptionString $errorMsg -ErrorCategory NotSpecified -ErrorID 1 -TargetObject $PSCmdlet $PSCmdlet.ThrowterminatingError($customError) } else { $userSMTPAddress = $nameResolutionCollection[0].Mailbox.Address } # Create a list of attendees $attendees = New-Object -TypeName "System.Collections.Generic.List[Microsoft.Exchange.WebServices.Data.AttendeeInfo]" $attnedee = New-Object -TypeName Microsoft.Exchange.WebServices.Data.AttendeeInfo -ArgumentList $userSMTPAddress $attendees.Add($attnedee) # Define a time window $timeWindow = New-Object -TypeName Microsoft.Exchange.WebServices.Data.TimeWindow -ArgumentList $StartDate, $EndDate $availabilityOptions = New-Object -TypeName Microsoft.Exchange.WebServices.Data.AvailabilityOptions $availabilityOptions.MeetingDuration = 60 # Call the availability service $userAvailabilityResults = $exService.GetUserAvailability($attendees,$timeWindow, ` [Microsoft.Exchange.WebServices.Data.AvailabilityData]::FreeBusyAndSuggestions, $availabilityOptions) # Output the availability information for each attendees foreach ($attendeesAvailability in $userAvailabilityResults.AttendeesAvailability) { if ($attendeesAvailability.ErrorCode -eq "NoError") { foreach ($calendarEvent in $attendeesAvailability.CalendarEvents) { $result = [PSCustomObject] @{ "FreeBusyStatus" = $calendarEvent.FreeBusyStatus; "StartTime" = $calendarEvent.StartTime; "EndTime" = $calendarEvent.EndTime; "Subject" = $calendarEvent.Details.Subject; "Location" = $calendarEvent.Details.Location; "IsException" = $calendarEvent.Details.IsException; "IsMeeting" = $calendarEvent.Details.IsMeeting; "IsPrivate" = $calendarEvent.Details.IsPrivate; "IsRecurring" = $calendarEvent.Details.IsRecurring; "IsReminderSet" = $calendarEvent.Details.IsReminderSet; } $PSCmdlet.WriteObject($result) } } else { $errMsg = New-OSCPSCustomErrorRecord -ExceptionString $attendeesAvailability.ErrorMessage -ErrorCategory NotSpecified -ErrorID 1 -TargetObject $PSCmdlet $PSCmdlet.WriteError($errMsg) } } } End {} } Export-ModuleMember -Function "Connect-OSCEXOWebService","Get-OSCEXOUserAvailability" |