opened
scott-lin
username_0
The following code should result in a console log with return code 2 instead of 0 if I'm not mistaken. How does tool runner get its return code for PowerShell invokes?

```
let tool: trm.ToolRunner;
tool = tl.tool(tl.which('PowerShell', true))
    .arg('-NoLogo')
    .arg('-NoProfile')
    .arg('-NonInteractive')
    .arg('-Command')
    .arg('${Exit 2}')
	
let options = <trm.IExecOptions>{ failOnStdErr: false, ignoreReturnCode: true };
let returnCode: number = await tool.exec(options);

console.log('PowerShell exited with return code: ' + returnCode);    // Result: "PowerShell exited with return code: 0"
```
toolrunner.exec does not return proper code with PowerShell invocation
issue
created
stephenmichaelf
username_1
@username_0 thanks for letting us know, I will take a look. May be a couple days.
comment
created
scott-lin
username_0
@username_1, any updated eta regarding this issue? Thanks.
comment
created
ericsciple
username_2
try this from command prompt:
```
D:\>powershell -nologo -noprofile -noninteractive -command "${Exit 2}"

D:\>echo %ERRORLEVEL%
0
```
comment
created
ericsciple
username_2
`${ }` is the value-of operator. Here is an example of it's use in powershell:

```powershell
PS C:\> ${Exit 2} = 'bar' ; dir variable:e* ; '-'*80 ;"The Exit 2 variable is: ${Exit 2}"

Name                           Value
----                           -----
Error                          {Cannot find drive. A drive with the name 'variables' does not exist.}
ErrorActionPreference          Continue
ErrorView                      NormalView
ExecutionContext               System.Management.Automation.EngineIntrinsics
Exit 2                         bar
--------------------------------------------------------------------------------
The Exit 2 variable is: bar
```
comment
closed
ericsciple
username_2
issue
created
scott-lin
username_0
Thanks for the learning, Eric. I am new to PowerShell, so I undoubtedly reported this issue with errors. Let me try again.

My typescript is ultimately executing the below. The command within the braces is referencing a script (ps1) with some arguments.

```
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoLogo -NoProfile -NonInteractive -Command "&{E:\agent_work\_tasks\BuildOpenApiDocument_106598a6-d5ba-4038-8dc8-ba0172210a94\0.0.21\Invoke-OpenApiCSharpCommentReader.ps1 -DependentAssembliesDirectoryPath 'E:\agent_work\_tasks\BuildOpenApiDocument_106598a6-d5ba-4038-8dc8-ba0172210a94\0.0.21' -OutputPath 'foo' -VisualStudioXmlDocumentationPath 'Bar.xml' -AssemblyPaths 'Baz.dll'}"
```

Based on the script's application logic, it will exit with a 0 for success, 1 for error, and 2 for warning. I would like to capture the exit code in typescript, so I can properly fail the task, etc.

Is there a way to accomplish this? Let me know if more details are needed.
comment
created
ericsciple
username_2
the step by step docs have an example https://github.com/Microsoft/vsts-task-lib/blob/master/node/docs/stepbystep.md#task-implementation
comment
created
scott-lin
username_0
Yes, that is the exact guide I followed initially. I'm reporting that the library does not function correctly for the situation laid out above. Can you repro?
comment
created
ericsciple
username_2
if we take your custom script out of the picture, and try with a simple case, are you able to repro? for example something like this:
```
powershell -nologo -noprofile -noninteractive -command "exit 2"
```

unit tests for this are passing: https://github.com/Microsoft/vsts-task-lib/blob/master/node/test/toolrunnertests.ts#L118
comment
created
scott-lin
username_0
I tried your repro, and it works as intended by vsts-task-lib.

Is there a unit test for my scenario? In other words, invoking a script file with a similar "-command" and returning an exit code.
comment
created
ericsciple
username_2
There is a unit test for validating the return code of a process. We don't have powershell specific unit tests, because as far as the tool runner is concerned, it's just a process like any other.

It feels like your question is about how to make your desired exit code bubble from a powershell script. And that is a generic powershell question, but this repo is about the task lib.
comment
created
scott-lin
username_0
Fair point. I'll move my inquiry over to StackOverflow. Thanks for the help.
comment
created
scott-lin
username_0
I've found the [solution to my PowerShell problem](https://stackoverflow.com/a/28722559/3897362), but encountered an [issue](https://github.com/Microsoft/vsts-task-lib/issues/312) in vsts-task-lib preventing me from using it. Hope this helps future readers.
comment