Author Posts

August 13, 2015 at 9:59 am

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?

August 13, 2015 at 10:20 am

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

August 13, 2015 at 10:23 am

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.

August 13, 2015 at 10:32 am

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>

August 13, 2015 at 11:04 am

Ah.

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

August 13, 2015 at 11:06 am

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

August 13, 2015 at 11:12 am

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.

August 13, 2015 at 3:27 pm

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

August 13, 2015 at 3:35 pm

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

August 13, 2015 at 4:24 pm

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