EWS API - For-Each using User Impersonation

New Contributor

Could someone show me how I would take this script and run it against a list of users? Currently it is only designed to run against the user which is authenticated.

I understand the on behalf permissions in Office 365 has to be granted, I just want to know how can I run this against a list of users, perhaps in a text file?

 

function Get-TeamsMeetingsFolder{
    param(
        [Parameter(Position = 1, Mandatory = $true)] [string]$MailboxName,
        [Parameter(Position = 2, Mandatory = $false)] [string]$AccessToken,
        [Parameter(Position = 3, Mandatory = $false)] [string]$url,
        [Parameter(Position = 4, Mandatory = $false)] [switch]$useImpersonation,
        [Parameter(Position = 5, Mandatory = $false)] [switch]$basicAuth,
        [Parameter(Position = 6, Mandatory = $false)] [System.Management.Automation.PSCredential]$Credentials,
        [Parameter(Position =7, Mandatory = $false) ]  [Microsoft.Exchange.WebServices.Data.ExchangeService]$service

    )
    Process {
        if($service -eq $null){
            if ($basicAuth.IsPresent) {
                if (!$Credentials) {
                    $Credentials = Get-Credential
                }
                $service = Connect-Exchange -MailboxName $MailboxName -url $url -basicAuth -Credentials $Credentials
            }
            else {
                $service = Connect-EXCExchange -MailboxName $MailboxName -ModernAuth  #-AccessToken $AccessToken
            }
            $service.HttpHeaders.Add("X-AnchorMailbox", $MailboxName);
            if ($useImpersonation.IsPresent) {
                $service.ImpersonatedUserId = new-object Microsoft.Exchange.WebServices.Data.ImpersonatedUserId([Microsoft.Exchange.WebServices.Data.ConnectingIdType]::SmtpAddress, $MailboxName)
            }
        }
        $folderid= new-object Microsoft.Exchange.WebServices.Data.FolderId([Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Root,$MailboxName)
        $TeamMeetingsFolderEntryId = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition([System.Guid]::Parse("{07857F3C-AC6C-426B-8A8D-D1F7EA59F3C8}"), "TeamsMeetingsFolderEntryId", [Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary);
        $psPropset= new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
        $psPropset.Add($TeamMeetingsFolderEntryId)
        $RootFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$folderid,$psPropset)
        $FolderIdVal = $null

        $TeamsMeetingFolder = $null
        if ($RootFolder.TryGetProperty($TeamMeetingsFolderEntryId,[ref]$FolderIdVal))
        {
            $TeamMeetingFolderId= new-object Microsoft.Exchange.WebServices.Data.FolderId((ConvertId -HexId ([System.BitConverter]::ToString($FolderIdVal).Replace("-","")) -service $service))
            $TeamsMeetingFolder = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($service,$TeamMeetingFolderId);
        }
        return $TeamsMeetingFolder


    }
}

 

1 Reply

Anyone have any ideas? Or even just an example of how I could store multiple users in the below function and carry it through?