title: PowerShell try/catch statement on olSync server
body: <p>I am working in PowerShell 2.0 on a olSync server connected to live@edu. I am trying to use a try/catch statement. </p>
  
  <p>In order to run on the live@edu server we have to run a script that uses a Import-PSSession command.</p>
  
  <p>My problem that I am having is that the try/catch statement works perfectly before I connect to the other session but after I connect the errors aren't caught and are displayed like they would normally.</p>
  
  <p>(what I mean by working perfectly is that they don't show up and my more user friendly response shows)</p>
  
  <p>What am I missing? Do I simply not understand the PSSession command enough?</p>
  
  <p><strong>Code for connecting (for obvious reasons I did not include the credentials)</strong></p>
  
  <pre><code>$Session = New-PSSession -ConfigurationName Microsoft.Exchange 
             -ConnectionUri https://ps.outlook.com/powershell/ -Credential $LiveCred 
            -Authentication Basic -AllowRedirection  
  
  Import-PSSession $Session
  </code></pre>
  
  <p><strong>Example code that I am testing</strong></p>
  
  <pre><code>$count = 0  
  $name = "quit"  
  $x  
  
  do  
  {   
  try    
  {  
      $name = Read-Host 'What is the username?'  
      $x = get-mailbox $name  
      write-host $x  
  }  
  catch  
  {  
         Write-Host "Oh No!`n $error[0].exception"  
         $count++  
  }  
  }  
  while ($name -ne "quit")  
  
  write-host "$count errors happened"  
  </code></pre>
  
  <p><strong>Output When connected</strong>  </p>
  
  <pre><code>What is the username?: test  
  test, test  
  
  What is the username?: test2  
  The operation couldn't be performed because object 'test2' couldn't be found on ...  
  
  What is the username?: quit  
  The operation couldn't be performed because object 'quit' couldn't be found on ...  
  
  0 errors happened  
  test  
  </code></pre>
  
  <p>.<br>
  <strong>Output When Not Connected</strong>  </p>
  
  <blockquote>
    <p>What is the username?: test<br>
    Oh No!<br>
    The term 'get-mailbox' is not recognized as the name of a cmdlet, function, script file,   > or operable program. Check the spelling of the name, or if a path was included, verify<br>
    that the path is correct and try again. The term 'get-mailbox'<br>
    is not recognized as the name of a cmdlet, function, script file, or operable program.<br>
    Check the spelling of the name, or if a path was included, verify that the<br>
    path is correct and try again.[0].exception  </p>
  </blockquote>