Getting error about use of & (ampersand) in script

Welcome Forums General PowerShell Q&A Getting error about use of & (ampersand) in script

This topic contains 9 replies, has 4 voices, and was last updated by

 
Participant
3 years, 3 months ago.

  • Author
    Posts
  • #28565
    GS

    Participant
    Points: 1
    Rank: Member

    Hello,

    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

    Keymaster
    Points: 1,624
    Helping HandTeam Member
    Rank: Community Hero

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

  • #28567

    Keymaster
    Points: 1,624
    Helping HandTeam Member
    Rank: Community Hero

    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
    GS

    Participant
    Points: 1
    Rank: Member

    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}
    SerializationVersion 1.1.0.1
    PSRemotingProtocolVersion 2.1

    PS C:\Windows\system32>

  • #28570

    Keymaster
    Points: 1,624
    Helping HandTeam Member
    Rank: Community Hero

    Ah.

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

  • #28571
    GS

    Participant
    Points: 1
    Rank: Member

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

  • #28572

    Keymaster
    Points: 1,624
    Helping HandTeam Member
    Rank: Community Hero

    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

    Participant
    Points: 5
    Rank: Member

    GS,

    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:
    2>&1

  • #28578
    GS

    Participant
    Points: 1
    Rank: Member

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

  • #28580

    Participant
    Points: 0
    Rank: Member

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

    "https://technet.microsoft.com/en-us/library/Hh847746(v=wps.620).aspx"
    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", "waka.do" | 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", "waka.do", "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\waka.do' because it does not exist.
    At line:1 char:64
    + $test = ("hotfix.html", "waka.do", "filter.xml" | Get-ChildItem < <<&1) + CategoryInfo : ObjectNotFound: (F:\temp\waka.do:String) [Get-ChildItem], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand -a--- 10/22/2014 11:10 AM 1201 filter.xml

The topic ‘Getting error about use of & (ampersand) in script’ is closed to new replies.