EWS API - For-Each using User Impersonation

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


    }
}

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

Techgeek4,
Welcome to the forum. :wave:t4:

I strongly doubt that it is meant to be used this way but the simplest and most straight forward way to use it for multiple users would be to use a CSV file with the needed input data and iterate through them calling the function for each of them individually.

Thanks. So let’s say for example I have this below. I know the Mailbox Name is one of the key factors. I also know that the " $service.HttpHeaders.Add(“X-AnchorMailbox”, $MailboxName); " is also key.

I am just wondering how I found fit this above into the If statement below?

If (Test-Path $CSVFileName) {

#Import the CSV file
$csvfile = Import-CSV $CSVFileName
    
#Loop through CSV file
foreach ($line in $csvfile) {

    try {
      

        WHAT WOULD MY SCRIPT NEED TO DO TO INPUT DATA FROM CSV?

    }
    catch {
        
        $message = "A problem occured trying to create the $($line.Name) contact"
        $message | Out-File $logfile -Append
        Write-Warning $message
        Write-Warning $_.Exception.Message
        $_.Exception.Message | Out-File $logfile -Append
    }

}

}

Since the MailboxName is the only mandatory parameter of the function I assume that it should run when you run it with an account having actually access to all the rest. :wink:
Then - assumend you have a CSV file with at least one column named MailboxName you could do it like this:

$csvfile = Import-CSV -Path $CSVFileName
foreach ($line in $csvfile) {
    Get-TeamsMeetingsFolder -MailboxName $($line.MailboxName)
}

If you need more than just the MailboxName you would need to have the other input data in your CSV file and add the other parameters accordingly.

Assuming you’re not the author of this function - how about asking the author for help?

Unfortunately, the author is not available. Just looking for a little bit of help. So you are saying that each time I have $Mailboxname, I will need to invoke those lines of code in order to bring in the value?

So in other words I might have multiple $CSVfile variables to ensure the name the mailbox name is carried through?

What if I wanted to pass the Mailbox name from the Powershell command line using the CSV and then run it for each? How would I do that? That might be easier actually.

.\Get-CallDetailRecording.ps1 -MailboxName first.last@domain.com -startdatetime ‘01/01/2022’ -enddatetime ‘01/31/2022’ | Export-CSV “Get-CallDetailRecording-TeamsMeetings-$((Get-Date).ToString(‘MM-dd-yyyy_hhmmtt’)).csv” -useculture -notypeinformation

Sorry if that sounds rude but it is beyond the scope of this forum (or any forum) to teach you a complex technology like scripting with PowerShell. And IMHO that is impossible as well by piecing together some arbitrary pieces of code you’ve found on the internet. Please start with learning the fundamentals of PowerShell first. It will save you from a lot of wasted time and frustrations.