powershell command

Bimal Karki 21 Reputation points
2022-08-11T23:42:08.117+00:00

Hi All,

I am trying to run the below command. It should get all the user mail boxes from the exchange server and run the script. But when I run it only does for the first mail box and stops. I want the script to run for each mailbox.

$mailboxes = Get-EXOMailbox -resultsize unlimited -RecipientTypeDetails usermailbox
Foreach($mailbox in $mailboxes){
.\graph-PopulateContactsFromCSV.ps1 -Mailbox $mailbox.UserPrincipalName -ClientSecret $clientSecret -ClientID $clientID -TenantID $tenantID -CSVPath $csvPath
}

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,479 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,344 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,361 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 44,776 Reputation points
    2022-08-12T02:13:18.95+00:00

    Add some code around that 3rd-party script to help figure out what's happening:

    $mailboxes = Get-EXOMailbox -ResultSize unlimited -RecipientTypeDetails usermailbox  
    $count = 0  
    $errors = 0  
    Write-Host "Will process $($mailboxes.count) mailboxes" -ForegroundColor Yellow  
    Foreach ($mailbox in $mailboxes) {  
        Write-Host "Begin processing $($mailbox.UserPrincipalName)" -ForegroundColor Yellow  
        .\graph-PopulateContactsFromCSV.ps1 -Mailbox $mailbox.UserPrincipalName -ClientSecret $clientSecret -ClientID $clientID -TenantID $tenantID -CSVPath $csvPath  
        if( -not $? )  
        {  
            $msg = $Error[0].Exception.Message  
            Write-Host "Encountered error during Deleting the Folder. Error Message is $msg. Please check." -ForegroundColor Red  
            $errors++  
        }  
        Write-Host "Finished processing $($mailbox.UserPrincipalName)"  
        $count++  
    }  
    Write-Host "Processed $count mailboxes"  
    Write-Host "There were $errors errors"  
    

    That 3rd-part script is already very noisy. It writes an awful lot of stuff to the console. It must be hard to find any errors in the noise.


1 additional answer

Sort by: Most helpful
  1. S.Sengupta 15,036 Reputation points MVP
    2022-08-12T00:48:29.093+00:00

    Kindly go through the following Microsoft reference and check if you are correct syntax or not...

    Get-Mailbox

    see also:

    get-mailbox : The term ‘get-mailbox’ is not recognized