Script Repository


Cancel meetings scheduled for user

February 14, 2019
1372

The script cancels meetings scheduled for a user according to the dates stored in specific properties of the user and then clear the properties.

Using the script, you can automatically release rooms tied up for meetings of a user who leaves your company. To do so, add the script to your deprovisioning process.

Parameters:

  • $startDateProperty - specifies a property of the user account that sets the start date starting from which meetings should be deleted
    • If you want to delete all meetings starting from the creation date of the user's mailbox, set the parameter to $NULL.
    • If you want to delete all meetings starting from the date when the script is executed on a user, set the parameter to "adm-CurrentDateTime".
  • $endDateProperty - specifies a property of the user account that sets the date up to which meetings should be deleted.
    • If you want to delete all meetings from the date specified in the $startDateProperty variable, set the parameter to $NULL.
    • If you want to delete all meetings up to the date when the script is executed on a user, set the parameter to "adm-CurrentDateTime".
    The properties specified in $startDateProperty and $endDateProperty must be of the DateTime type. We suggest using Adaxes custom properties for this purpose, for example, CustomAttributeDate1 andCustomAttributeDate2.
  • $exchangeServer - specifies the Fully Qualified Domain Name (FQDN) of the Exchange Server that will be used by the script (only for Exchange On-Premises). If the script is used for Exchange Online only, set the variable to $NULL.
  • $exchangeWebServiceDllPath - specifies the full path to the Microsoft Exchange Web Services DLL module.
    To be able to run the script, download and install Microsoft Exchange Web Services Managed API on the computer where Adaxes Service runs. The DLL module will be installed as a part of the Exchange Web Services API.
Edit Remove
PowerShell
$startDateProperty = "adm-CustomAttributeDate1" # TODO: modify me
$endDateProperty = "adm-CustomAttributeDate2" # TODO: modify me
$exchangeWebServiceDllPath = "C:\Program Files\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll" # TODO: modify me

Import-Module $exchangeWebServiceDllPath

function GetDates ($property, $addDays)
{
    try
    {
        $value = $Context.TargetObject.Get($property)
        if ($value -is [Softerra.Adaxes.Adsi.AdsLargeInteger])
        {
            $value = [DateTime]::FromFiletime([Int64]::Parse($value))
        }
        $value = New-Object "System.Datetime" $value.Year, $value.Month, ($value.Day + $addDays), 0, 0, 0, ([System.DateTimeKind]::Local)
    }
    catch
    {
        $value = $NULL
    }
    
    return $value
}

function ClearProperties ($propertiesName)
{
    foreach ($name in $propertiesName)
    {
        if (($name -eq $NULL) -or 
            ($name -eq "adm-CurrentDateTime"))
        {
            continue
        }
        
        $Context.TargetObject.Put($name, $NULL)
        $Context.TargetObject.SetInfoEx(@($name))
    }
}

# Check whether the user has mailbox in Exchange Online
if (-not($Context.TargetObject.RecipientType -eq "ADM_EXCHANGERECIPIENTTYPE_MAILBOXENABLED" -and `
    $Context.TargetObject.RecipientLocation -eq "ADM_EXCHANGERECIPIENTLOCATION_OFFICE365"))
{
    return
}

# Get search parameters
$startDate = GetDates $startDateProperty 0
$endDate = GetDates $endDateProperty 1

# Get primary SMTP Address
$mailboxParams = $Context.TargetObject.GetMailParameters()
$emailAddresses = $mailboxParams.EmailAddresses
$primarySMTPAddress = $NULL

for ($i = 0; $i -lt $emailAddresses.Count; $i++)
{
    $emailAddress = $emailAddresses.GetAddress($i,[ref]"ADS_PROPERTY_NONE")
    if ($emailAddress.IsPrimary -and $emailAddress.Prefix -eq "smtp")
    {
        $primarySMTPAddress = $emailAddress.Address
        break
    }
}

if ([System.String]::IsNullOrEmpty($primarySMTPAddress))
{
    $Context.LogMessage("Cannot remove meetings scheduled by the user because the user's mailbox doesn't have a primary SMTP address.", "Warning")
    return
}

# Connect to Exchange Online via the Exchange Web Services API
$exchangeWebService = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService
$office365Cred = $Context.GetOffice365Credential()
$exchangeWebService.Credentials = New-Object System.Net.NetworkCredential($office365Cred.Username, $office365Cred.GetNetworkCredential().Password)
$exchangeWebService.Url = "https://outlook.office365.com/EWS/Exchange.asmx"

# Get the user's calendar
$calendarFolderId = New-Object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Calendar, $primarySMTPAddress)
$calendarFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($exchangeWebService, $calendarFolderId)
$itemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView(500)
    
$propertySet = New-Object Microsoft.Exchange.WebServices.Data.PropertySet(
    [Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties, 
    [Microsoft.Exchange.WebServices.Data.AppointmentSchema]::Organizer)

do
{
    $searchResult = $calendarFolder.FindItems($itemView)
    foreach ($item in $searchResult.Items)
    {
        if (!($item.IsMeeting))
        {
            continue # Not a meeting
        }
        
        $item.Load($propertySet)
        
        if ($startDate -eq $NULL -and $endDate -eq $NULL)
        {
            $item.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)
        }
        elseif (($startDate -eq $NULL) -and
            ($item.Start -lt $endDate))
        {
            $item.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)
        }
        elseif (($endDate -eq $NULL) -and 
            ($item.Start -ge $startDate))
        {
            $item.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)
        }
        elseif (($startDate -ne $NULL) -and 
            ($endDate -ne $NULL) -and 
            ($item.Start -ge $startDate) -and 
            ($item.Start -lt $endDate))
        {
            $item.Delete([Microsoft.Exchange.WebServices.Data.DeleteMode]::HardDelete)
        }
    }
    
    $itemView.Offset = $searchResult.NextPageOffset
}
while($searchResult.MoreAvailable -eq $True)

# Clear date properties
ClearProperties @($startDateProperty, $endDateProperty)

Comments ( 0 )
No results found.
Leave a comment