Mid size tenant powershell times out w/ access denied

jpcapone 1,491 Reputation points
2021-01-30T05:42:02.923+00:00

I have cobbled together two scripts from here

https://www.thelazyadministrator.com/2018/06/22/create-an-interactive-html-report-for-office-365-with-powershell/ and here https://www.linkedin.com/pulse/documenting-your-office-365-tenant-suzanne-hunt

Now the script runs successfully against a small tenant but against a larger tenant it always ends with the error below. I am launching an elevated powershell session, using a GA account and I confirmed access to the tenant. Any suggestions on how I can get this script to run successfully?

Starting a command on the remote server failed with the following error message : Access is denied. For more information, see the about_Remote_Troubleshooting Help topic.

  • CategoryInfo : OperationStopped: (outlook.office365.com:String) [], PSRemotingTransportException
  • FullyQualifiedErrorId : JobFailure
  • PSComputerName : outlook.office365.com

Starting a command on the remote server failed with the following error message : The I/O operation has been aborted because of either a thread exit or an application request. For more information, see the about_Remote_Troubleshooting Help topic.

  • CategoryInfo : OperationStopped: (outlook.office365.com:String) [], PSRemotingTransportException
  • FullyQualifiedErrorId : JobFailure
  • PSComputerName : outlook.office365.com
Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
547 questions
0 comments No comments
{count} votes

Accepted answer
  1. KyleXu-MSFT 26,266 Reputation points
    2021-02-01T06:20:43.333+00:00

    @jpcapone

    The author post this script on his GitHub, you can report this issue to bwya77, check whether he could add a timeout parameter to his script.

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Vasil Michev 107.7K Reputation points MVP
    2021-01-30T08:20:10.027+00:00

    That's a common issue with running Exchange Remote PowerShell, you need to add some code to handle throttling and session reconnects. Lots of good suggestions here: https://techcommunity.microsoft.com/t5/exchange-team-blog/running-powershell-cmdlets-for-large-numbers-of-users-in-office/ba-p/604280


  2. jpcapone 1,491 Reputation points
    2021-01-31T21:20:07.52+00:00

    here is the snippet in the script that i think cause the time out

    Write-Host "Getting Room Mailboxes..." -ForegroundColor Yellow
    $Rooms = Get-Mailbox -ResultSize Unlimited -Filter '(RecipientTypeDetails -eq "RoomMailBox")'
    Foreach ($Room in $Rooms)
    {
    $RoomArray = New-Object 'System.Collections.Generic.List[System.Object]'

    $RoomName = $Room.DisplayName
    $RoomPrimEmail = $Room.PrimarySmtpAddress
    $RoomEmails = ($Room.EmailAddresses | Where-Object { $_ -cnotmatch '^SMTP' })
    foreach ($RoomEmail in $RoomEmails)
    {
        $RoomEmailSplit = $RoomEmail -split ":" | Select-Object -Last 1
        $RoomArray.add($RoomEmailSplit)
    }
    

    DOES THE BELOW SEEM RIGHT

    Start-RobustCloudCommand Write-Host "Getting Room Mailboxes..." -ForegroundColor Yellow
    $Rooms = Get-Mailbox -ResultSize Unlimited -Filter '(RecipientTypeDetails -eq "RoomMailBox")'
    Foreach ($Room in $Rooms)
    {
    $RoomArray = New-Object 'System.Collections.Generic.List[System.Object]'

    $RoomName = $Room.DisplayName
    $RoomPrimEmail = $Room.PrimarySmtpAddress
    $RoomEmails = ($Room.EmailAddresses | Where-Object { $_ -cnotmatch '^SMTP' })
    foreach ($RoomEmail in $RoomEmails)
    {
        $RoomEmailSplit = $RoomEmail -split ":" | Select-Object -Last 1
        $RoomArray.add($RoomEmailSplit)
    }
    
    0 comments No comments

  3. Rich Matheisen 46,811 Reputation points
    2021-01-31T22:42:17.09+00:00

    This code may not entirely address your problem, but it should begin processing the mailboxes before the Get-Mailbox cmdlet finishes, cutting the elapsed time.

    Write-Host "Getting Room Mailboxes..." -ForegroundColor Yellow
    Get-Mailbox -ResultSize Unlimited -Filter '(RecipientTypeDetails -eq "RoomMailBox")' |
        Foreach-Object{
            $RoomArray = New-Object 'System.Collections.Generic.List[System.Object]'
    
            $RoomName = $_.DisplayName
            $RoomPrimEmail = $_.PrimarySmtpAddress
            $Room.EmailAddresses | Where-Object { $_ -cnotmatch '^SMTP' } | # might be slightly faster to use "-cnotlike 'SMTP:*' ???"
                ForEach-Object{
                    $RoomArray.add(($RoomEmail -split ":")[-1])
                }
            # continue processing this mailbox
        }
    

    Another thing that may help is to segregate your mailboxes and place those room mailboxes in another OU. Add a search scope to the Get-Mailbox and eliminate the need to look all the 'other' types of mailboxes.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.