Getting error about use of & (ampersand) in script

This topic contains 9 replies, has 4 voices, and was last updated by  Curtis Smith 2 years, 9 months ago.

  • Author
  • #28565



    I'm getting error below

    PS C:\Windows\system32> Import-Module MyWebAdministration
    Ampersand not allowed. The & operator is reserved for future use; use "&" to pass ampersand as a string.
    At \\prod\serverops\BuildStandards\deployment\modules\MyWebAdministration\scripts\cleanup.ps1:129 char:40
    + LogOutput ($Sender | Receive-Job *>& < <<< 1) + CategoryInfo : ParserError: (:) [], ParseException + FullyQualifiedErrorId : AmpersandNotAllowed

    The offending code is below

    LogOutput ($Sender | Receive-Job *>&1)

    I believe I'm using redirection properly, in fact this is example taken from "about_redirection" helpfile
    What gives?

  • #28566

    Don Jones

    You can't really do redirection in a subexpression in quite that way, I don't think. Give me a sec to experiment.

  • #28567

    Don Jones

    Doing something like:

    $x = ("localhost" | get-service *>&1)

    Seems to work OK. You might try doing that – capturing it into a variable, and then passing the variable. I obviously can't experiment with your exact code, but the above worked for me.

  • #28569


    This did not work, my code is below and still the same issue. This happens during module-import, not actual job run

    $temp = ($Sender | Receive-Job *>&1)
    LogOutput ($temp)

    PS C:\Windows\system32> $PSVersionTable

    Name Value
    —- —–
    CLRVersion 2.0.50727.5485
    BuildVersion 6.1.7601.17514
    PSVersion 2.0
    WSManStackVersion 2.0
    PSCompatibleVersions {1.0, 2.0}
    PSRemotingProtocolVersion 2.1

    PS C:\Windows\system32>

  • #28570

    Don Jones


    That redirection doesn't exist in v2. It was introduced in v3.

  • #28571


    What would be the solution for V2 if you want to redirect both errors and not errors of command to same output?

  • #28572

    Don Jones

    Unfortunately, the solution is to upgrade to v3. v2 doesn't really provide the same pipe redirection capability. You'll need to look at capturing errors some other way.

  • #28577

    Tim Curwick


    Some redirection did not exist in 2.0, including the all * notation you are using, but I believe it is possible to redirect the error stream. If that's all you need, try:

  • #28578


    I need both error and success stream to go to the same output.

  • #28580

    Curtis Smith

    Tim is right if you only need the error and success streams

    2>&1 Sends errors (2) and success output (1) to the success output stream.

    Where *>&1 returns your original error:

    PS F:\temp> $test = "hotfix.html", "" | Get-ChildItem *>&1
    & : Ampersand not allowed. The & operator is reserved for future use; use "&" to pass ampersand as a string.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : AmpersandNotAllowed

    2>&1 will place your error messages in your standard output stream:
    Below I am storing that returned value in a variable as Don mentioned.

    PS F:\temp> $test = ("hotfix.html", "", "filter.xml" | Get-ChildItem 2>&1)
    PS F:\temp> $test

    Directory: F:\temp

    Mode LastWriteTime Length Name
    ---- ------------- ------ ----
    -a--- 7/28/2015 8:14 AM 628502 hotfix.html
    Get-ChildItem : Cannot find path 'F:\temp\' because it does not exist.
    At line:1 char:64
    + $test = ("hotfix.html", "", "filter.xml" | Get-ChildItem < <<&1) + CategoryInfo : ObjectNotFound: (F:\temp\ [Get-ChildItem], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand -a--- 10/22/2014 11:10 AM 1201 filter.xml

You must be logged in to reply to this topic.