title: Capturing powershell warning
body: <p>I am trying to capture the warning that gets thrown if I try to remove mailbox permissions from a mailbox for someone who doesn't have permissions in the first place.</p>
  
  <pre><code>#$WarningPreference = "continue"
  try
  {
  Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
  Remove-MailboxPermission -Identity "test2" -User "test1" -AccessRights FullAccess -InheritanceType All -confirm:$False | Out-File c:\temp\output2.txt -Encoding ASCII
  }
  catch
  {
  #Write-Warning -Message $($_.Exception.Message) 3 &gt; c:\temp\warning.txt
  }
  </code></pre>
  
  <p>There is no output in either output2.txt or warning.txt - what am I doing wrong?</p>
  
  <p>The warnings I am trying to capture appear in yellow and say:</p>
  
  <pre><code>WARNING: The cmdlet extension agent with the index 0 has thrown an exception in OnComplete(). The exception is:
  System.InvalidOperationException: Operation is not valid due to the current state of the object.
     at Microsoft.Exchange.Data.Storage.ExchangePrincipal.get_ServerFullyQualifiedDomainName()
     at Microsoft.Exchange.Data.Storage.MailboxSession.Initialize(MapiStore linkedStore, LogonType logonType,
  ExchangePrincipal owner, DelegateLogonUser delegateUser, Object identity, OpenMailboxSessionFlags flags,
  GenericIdentity auxiliaryIdentity)
     at Microsoft.Exchange.Data.Storage.MailboxSession.&lt;&gt;c__DisplayClass12.&lt;CreateMailboxSession&gt;b__10(MailboxSession
  mailboxSession)
     at Microsoft.Exchange.Data.Storage.MailboxSession.InternalCreateMailboxSession(LogonType logonType,
  ExchangePrincipal owner, CultureInfo cultureInfo, String clientInfoString, IAccountingObject budget, Action`1
  initializeMailboxSession, InitializeMailboxSessionFailure initializeMailboxSessionFailure)
     at Microsoft.Exchange.Data.Storage.MailboxSession.ConfigurableOpen(ExchangePrincipal mailbox, MailboxAccessInfo
  accessInfo, CultureInfo cultureInfo, String clientInfoString, LogonType logonType, PropertyDefinition[]
  mailboxProperties, InitializationFlags initFlags, IList`1 foldersToInit, IAccountingObject budget)
     at Microsoft.Exchange.Data.Storage.MailboxSession.OpenAsSystemService(ExchangePrincipal mailboxOwner, CultureInfo
  cultureInfo, String clientInfoString)
     at Microsoft.Exchange.ProvisioningAgent.MailboxLoggerFactory.XsoMailer.Log(AdminLogMessageData data,
  LogMessageDelegate logMessage)
     at Microsoft.Exchange.ProvisioningAgent.AdminLogProvisioningHandler.OnComplete(Boolean succeeded, Exception e)
     at Microsoft.Exchange.Provisioning.ProvisioningLayer.OnComplete(Task task, Boolean succeeded, Exception exception)
  WARNING: Can't remove the access control entry on the object "CN=xxxxx" for account "xxxxx" because the ACE doesn't exist on the
  object.
  </code></pre>
  
  <p>Thanks for all the help so far!  My code now looks like this:</p>
  
  <pre><code>$WarningPreference = "continue"
  try
  {
  Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
  Remove-MailboxPermission -Identity "test1" -User "test2" -AccessRights FullAccess -InheritanceType All -confirm:$False 2&gt; c:\temp\errors.txt 3&gt; c:\temp\warnings.txt -ErrorAction Stop
  }
  catch
  {
  2&gt; c:\temp\errors.txt
  }
  </code></pre>
  
  <p><strong>*FINAL SOLUTION - THANKS ALL *</strong></p>
  
  <pre><code>$WarningPreference = "continue"
  try
  {
  Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010
  Remove-MailboxPermission -Identity "test1" -User test2" -AccessRights FullAccess -InheritanceType All -confirm:$False 2&gt; c:\temp\errors.txt 3&gt; c:\temp\warnings.txt -ErrorAction Stop
  }
  catch
  {
  $_ &gt; c:\temp\errors.txt
  }
  </code></pre>