0

I need to pull all forwarding rules for an exchange online environment, and output them to a csv. this sounds simple, but I have an additional caveat. there are 23,000 mailboxes in the org.

I was able to write the script I needed, it outputted the data, but it timed out. then I was able to break out only certain mailboxes that were critical (11,000) but I was still timing out in powershell.

so finally, I found an article that detailed breaking up a script into blocks of 1,000, and running numerous sessions. and runs! it runs without timing out. but it doesn't output to the csv anymore. since my script has gone through several iterations, I'm pretty sure that my problem is the way I'm storing, or outputting the array, but for all my staring at this, I cant figure it out. short of asking the doc for a prescription of Adderall, I figured id ask here. below is the offending script.

the aliaslist.csv that it mentions is just a csv with a list of aliases for 11,000 mailboxes. if you would like to run your own tests, you can adjust $pagesize down and paste a few mailboxes into a csv called aliaslist, stored in c:\temp

    Function New-O365ExchangeSession()

{

param( 

[parameter(mandatory=$true)]

$365master)

#close any old remote session

Get-PSSession | Remove-PSSession -Confirm:$false

#start a new office 365 remote session

$365session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $365master -Authentication Basic -AllowRedirection

$office365 = Import-PSSession $365session

}



#set input variables
$path = "C:\temp"
$InputFile = aliaslist.csv"
$UserEmail = "[email protected]"

#set variables for csv usage
$Offset = 0;
$PageSize = 1000;
$MbxMax = (Import-Csv "$path/$InputFile").count



#Loop in the list and retrieve the device’s information

$file = “c:\temp\office365-$((get-date).tostring(“yyyy-MM-dd”)).csv”

$365master = get-credential $UserEmail

New-O365ExchangeSession $365master 
# call the office365 remote connection function


do{
$mbxlist=@(import-csv "$path/$InputFile"|select-object -skip $Offset -First $PageSize)

"Process entry $($Offset) to $($Offset+$PageSize)"

#end csv input count reference


ForEach($mbx in $MbxList)

    {
    #Write to Host
    "start Processing $($mbx.alias)"
    #end Write to host, 





        #Check rules
        $rules = Get-InboxRule -mailbox $_.alias | ? {$_.RedirectTo -ne $null -or $_.ForwardTo -ne $null -or $_.ForwardAsAttachmentTo -ne $null}
        If ($rules -ne $null)
        {
            $rules | % {

                #check for forwardAsAttachments
                If ($_.ForwardAsAttachmentTo -ne $null)
                {
                    $obj = New-Object system.object
                    $obj | Add-Member -name "NetID" -Value $_.alias -MemberType NoteProperty
                    $obj | Add-Member -name "ForwardType" -Value "Forward As Attachment Rule" -MemberType NoteProperty
                    $obj | Add-Member -name "ForwardAddress" -Value $_.forwardAsAttachmentTo -MemberType NoteProperty
                    $obj | Add-Member -name "Enabled" -Value $_.Enabled -MemberType NoteProperty
                    $obj | Add-Member -name "Description" -Value $f -MemberType NoteProperty

                        If (Test-Path $file)
                       {
                             $mbx.alias + ”,” + ($obj | ConvertTo-Csv)[2] | Out-File $file –Append
                          }
                         Else
                       {
                      $obj | Export-Csv $file -Encoding ASCII -notypeinformation
                     }
                }
                $obj = $null


                #check for redirects
                If ($_.redirectto -ne $null)
                {
                    $obj = New-Object system.object
                    $obj | Add-Member -name "NetID" -Value $_.alias -MemberType NoteProperty
                    $obj | Add-Member -name "ForwardType" -Value "Redirct Rule" -MemberType NoteProperty
                    $obj | Add-Member -name "ForwardAddress" -Value $_.redirectto -MemberType NoteProperty
                    $obj | Add-Member -name "Enabled" -Value $_.Enabled -MemberType NoteProperty
                    $obj | Add-Member -name "Description" -Value $c -MemberType NoteProperty

                        If (Test-Path $file)

                       {
                             $mbx.alias + ”,” + ($obj | ConvertTo-Csv)[2] | Out-File $file –Append
                          }
                         Else
                       {
                      $obj | Export-Csv $file -Encoding ASCII -notypeinformation
                     }
                }   
                $obj = $null


            #check for forwards
                If ($_.ForwardTo -ne $null)
                {
                    $obj = New-Object system.object
                    $obj | Add-Member -name "NetID" -Value $_.alias -MemberType NoteProperty
                    $obj | Add-Member -name "ForwardType" -Value "Forward Rule" -MemberType NoteProperty
                    $obj | Add-Member -name "ForwardAddress" -Value $_.forwardto -MemberType NoteProperty
                    $obj | Add-Member -name "Enabled" -Value $_.Enabled -MemberType NoteProperty
                    $obj | Add-Member -name "Description" -Value $f -MemberType NoteProperty    
                        If (Test-Path $file)
                       {
                             ($obj | ConvertTo-Csv)[2] | Out-File $file –Append
                          }
                         Else
                       {
                      $obj | Export-Csv $file -Encoding ASCII -notypeinformation
                     }
                }   
                $obj = $null
            }       
        }
         }






#increment the start point for the next chunk
$Offset+=$PageSize


#Call the office365 remote session function to close the current one and open a new session

New-O365ExchangeSession $365master

} while($Offset -lt $MbxMax)
7
  • Have you tried adjusting the OperationTimeout? The default is 3 minutes. This is probably the timeout that kills your script. Commented Feb 21, 2017 at 15:21
  • I havnt, its for office 365, I'm not sure you are allowed to change the timeout in o365 are you? Commented Feb 21, 2017 at 16:16
  • It is a PSSession setting, could be worth a try tough. Commented Feb 21, 2017 at 16:18
  • ok, so I wasn't able to find any timeout adjust solutions when I was first researching, but then you posted it, so I thought I might have missed it. I searched again, and found this: social.technet.microsoft.com/Forums/en-US/… and this: powershelladmin.com/wiki/… both seem to imply that Its not practical for my situation. Commented Feb 21, 2017 at 16:32
  • but then I found this: stackoverflow.com/questions/19855822/powershell-command-timeout and it mentions doing background runspaces. between me and you, id much rather fix my array issue than rewrite the whole thing again to use background runspaces. Commented Feb 21, 2017 at 16:35

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.