locked
Import-PSSession -WarningAction not working as I thought RRS feed

  • Question

  • In a previous post (Warning when using exchange snap-in in PowerShell) I was asking how to properly connect to Exchange 2010 via powershell in a script with no output to the user because I am outputting Write-Host commands to the user and I don't want the warnings or output text from the Import-PSSession to interfere with my text being output to the user.  We have since figured it out, but I am curious as to why my examples below do or do not work.

    This DOES WORK:

    $WarningPreference = "SilentlyContinue"
    $session = New-PSSession `
    -ConfigurationName Microsoft.Exchange `
    -ConnectionUri http://server.domain.com/PowerShell/ `
    -Authentication Kerberos
    Import-PSSession $session | Out-Null

    Does NOT WORK:

    $session = New-PSSession `
    -ConfigurationName Microsoft.Exchange `
    -ConnectionUri http://server.domain.com/PowerShell/ `
    -Authentication Kerberos
    Import-PSSession $session -WarningAction SilentlyContinue | Out-Null

    Also does NOT WORK:

    $session = New-PSSession `
    -ConfigurationName Microsoft.Exchange `
    -ConnectionUri http://server.domain.com/PowerShell/ `
    -Authentication Kerberos
    Import-PSSession $session -WarningAction "SilentlyContinue" | Out-Null


    Can anyone explain why changing the $WarningPreference to "SilentlyContinue"  DOES work, but running "Import-PSSession $session -WarningAction SilentlyContinue" does NOT?  Is my command not typed properly?


    Find this post helpful? Does this post answer your question? Be sure to mark it appropriately to help others find answers to their searches.

    Sunday, March 23, 2014 1:30 PM

Answers

  • The problem is that you are importing a session which actually sends remote commands and there are two.  Apparently one remote command does not honor the local setting when it is not global.  Setting the global version does affect both steps.

    Placing quotes around argument options is never required and is no different from no quotes except when there are spaces inn the option string.


    ¯\_(ツ)_/¯

    • Marked as answer by Chase Roth Monday, March 24, 2014 9:44 AM
    Sunday, March 23, 2014 2:10 PM

All replies

  • Guess a bit better search would have helped...

    http://social.technet.microsoft.com/Forums/windowsserver/en-US/19d27481-1acc-412c-9085-9f89d00fa4e9/powershell-v2-ctp3-advanced-function-parameter-warningaction-not-working?forum=winserverpowershell

    http://technet.microsoft.com/en-us/library/hh847781.aspx

    Although I am not really understanding what I am reading at this point.  I'll check a bit more into this and try and read some more.


    Find this post helpful? Does this post answer your question? Be sure to mark it appropriately to help others find answers to their searches.

    Sunday, March 23, 2014 1:37 PM
  • The problem is that you are importing a session which actually sends remote commands and there are two.  Apparently one remote command does not honor the local setting when it is not global.  Setting the global version does affect both steps.

    Placing quotes around argument options is never required and is no different from no quotes except when there are spaces inn the option string.


    ¯\_(ツ)_/¯

    • Marked as answer by Chase Roth Monday, March 24, 2014 9:44 AM
    Sunday, March 23, 2014 2:10 PM
  • The problem is that you are importing a session which actually sends remote commands and there are two.  Apparently one remote command does not honor the local setting when it is not global.  Setting the global version does affect both steps.

    Placing quotes around argument options is never required and is no different from no quotes except when there are spaces inn the option string.


    ¯\_(ツ)_/¯


    Thanks, that is a simple and effective explanation.

    Find this post helpful? Does this post answer your question? Be sure to mark it appropriately to help others find answers to their searches.

    Monday, March 24, 2014 9:44 AM