Hello everyone,
I am encountering issues with Remote Exchange sessions and was wondering what I am overlooking/over-complicating.
I am running Windows 8.1/PowerShell 4.0. Reason I mention this is because of this blog post:
http://blogs.msdn.com/b/powershell/archive/2013/10/25/windows-management-framework-4-0-is-now-available.aspx
The IMPORTANT section noted that Exchange 2007, 2010, 2013 are not compatible with WMF 4.0 and didn't know if that is good/bad since the OS is using PowerShell 4.0.
1 - I am connecting to Office 365 utilizing the following:
http://help.outlook.com/en-us/140/cc952755.aspx
2 - I have the following function:
function Get-SomeMailboxStatistics { [CmdletBinding()] param( [Parameter(Mandatory=$True, ValueFromPipeline=$True, HelpMessage="Identities or aliases to gather Exchange Statistics from.")] [Alias('Username')] [string[]]$Identity, [string]$ErrorLog = 'C:\MbxStatsErrorLog.txt', [switch]$LogErrors ) BEGIN{} PROCESS{ Write-Verbose "Beginning..." foreach ($alias in $Identity) { Write-Verbose "Gathering info for $alias." #trap [ManagementObjectNotFoundException] {$everythingOK = $false} Try{ # $ErrorActionPreference = "Stop" # Testing purposes. $everythingOK = $true $gms = Get-MailboxStatistics -Identity $alias -ErrorAction Stop # http://technet.microsoft.com/library/hh847884.aspx # Trap { # throw $_ # } } Catch{ $everythingOK = $false Write-Warning "Lookup on $alias failed." if ($LogErrors -eq $true){ $alias | Out-File $ErrorLog -Append Write-Warning "Logged $alias to $ErrorLog." } } if ($everythingOK){ $props = @{}; $props = @{'Alias' = $alias; 'DisplayName' = $gms.DisplayName; 'TotalItemSizeInBytes' = ($gms.TotalItemSize.Value.ToString() -replace "(.*\()|,| [a-z]*\)", ""); 'TotalDeletedItemSizeInBytes' = ($gms.TotalDeletedItemSize.Value.ToString() -replace "(.*\()|,| [a-z]*\)", ""); 'MailboxType' = $gms.MailboxType;
}; $obj = New-Object -TypeName PSObject -Property $props Write-Output $obj } } } END{} }
Example: Get-SomeMailboxStatistics -Identity GoodAlias1,GoodAlias2,FailAlias,GoodAlias3 -LogErrors -ErrorLog 'C:\Logs\MbxStatsErrors.txt' -Verbose
The pipeline terminates on FailAlias, and then throws an error when it tries to pass the FailAlias to the $props.
$gms | Get-Member on a good alias is:
TypeName: Deserialized.Microsoft.Exchange.Management.MapiTasks.Presentation.MailboxStatistics
I have tried setting $ErrorActionPreference = 'Stop' in various sections and think I am scope-creeping/not catching the Error where I should be or PowerShell is flat-out ignoring something.
I have tried additional nested if{}else{} in the Try{}Catch{} and cannot get it to behave as expected.
I have also tried to Trap{[ManagementObjectNotFoundException]} and Throw exception in various portions without success.
However, setting $ErrorActionPreference = 'Stop' in the session window (from default of 'Continue') the script behaves as I expected it to, when encountering a non-existant mailbox, it logs the error. It doesn't seem to have issues executing on Windows 8.0/PowerShell
3.0.
I have encountered numerous blog/forum posts with no definitive answer/resolution.
Based some of my tests around some suggestions from this thread:
http://stackoverflow.com/questions/1142211/try-catch-does-not-seem-to-have-an-effect
Extras:
http://blogs.technet.com/b/heyscriptingguy/archive/2010/03/09/hey-scripting-guy-march-9-2010.aspxhttp://stackoverflow.com/questions/19553278/powershell-catch-non-terminating-errors-with-silentlycontinue
http://stackoverflow.com/questions/15545429/erroractionpreference-and-erroraction-silentlycontinue-for-get-pssessionconfigur
http://social.technet.microsoft.com/Forums/scriptcenter/en-US/228a3329-f564-4daa-ad70-6d869b912246/non-terminating-error-turned-into-a-terminating-error?forum=winserverpowershell