PowerShell Task: Parameters not passed to PowerShell script (Bug #261)


Added by Aaron Jensen over 4 years ago. Updated over 3 years ago.


Status:Feedback Start date:02/08/2013
Priority:Normal Due date:
Assignee:Ruben Willems % Done:

0%

Category:Task Spent time: -
Target version:1.9
Affected version:1.8.3

Description

If a PowerShell task has a script with parameters (passed via the <buildArgs> configuration option), those parameters are not passed to the PowerShell script. All PowerShell scripts are essentially being run without parameters.

For example,

&lt;powershell>
&lt;buildArgs>-Parameter1 Value1 -Switch&lt;/buildArgs>
&lt;buildTimeoutSeconds>600&lt;/buildTimeoutSeconds>
&lt;scriptsDirectory>C:\Scripts&lt;/scriptsDirectory>
&lt;dynamicValues />
&lt;environment />
&lt;executable>powershell.exe&lt;/executable>
&lt;priority>Normal&lt;/priority>
&lt;script>Invoke-MyBuild.ps1&lt;/script>
&lt;successExitCodes />
&lt;/powershell>

CCNet runs the task above like this:

powershell.exe -file C:\Scripts\Invoke-MyBuild.ps1 -Parameter1 Value1 -Switch

This is incorrect. PowerShell's `-File` parameter doesn't support passing parameters to the file/script, so the `-Parameter1 Value -Switch` are interpreted as parameters to powershell.exe, which are ignored.

The correct syntax is to use the `-Command` parameter:

powershell.exe -Command C:\Scripts\Invoke-MyBuild.ps1 -Parameter1 Value1 -Switch

Mercurial.cs (32.8 kB) Aaron Jensen, 02/08/2013 03:52 am

MercurialTest.cs (17.1 kB) Aaron Jensen, 02/08/2013 03:52 am

PowerShellTaskTest.cs (13 kB) Aaron Jensen, 02/08/2013 03:55 am

PowerShellTask.cs (17 kB) Aaron Jensen, 02/08/2013 03:55 am


History

Updated by Aaron Jensen over 4 years ago

Here is the code fix, with corresponding unit test changes.

Updated by Aaron Jensen over 4 years ago

Oops. WRong files. Grrr. Here are the correct ones.

Updated by Dima B over 4 years ago

Aaron Jensen,
here is part of my config where I use powershell. As you can see, I use here -file and pass to my ps1 file parameter "$(TestsFolder)\Test Results". And it works, ps1 script receives passed parameter.

<exec>
    <executable>$(PS)</executable>
    <baseDirectory>$(TestsFolder)\Test Results\Core.Tests.$(CfgName)\</baseDirectory>
    <buildArgs>-file "$(CCNetSrvFolder)\Delete_Old_TestResults.ps1" "$(TestsFolder)\Test Results"</buildArgs>
</exec>

Updated by Aaron Jensen over 4 years ago

You're using the exec task. Please duplicate with the powershell task. I can assure you, none of my PowerShell scripts were getting passed arguments.

Updated by Ruben Willems about 4 years ago

  • Target version set to 1.8.5

Updated by Ruben Willems about 4 years ago

there is another issue with powershell: #297
I've commented on that one
Can you follow that one, since I want to get this solved for the 1.8.5

  • Category set to Task
  • Status changed from New to Feedback
  • Assignee set to Daniel Nauck

Updated by Ruben Willems about 4 years ago

  • Assignee changed from Daniel Nauck to Ruben Willems

Updated by Olivier Sannier about 4 years ago

Here is my config

    <powershell>
      <script>DeleteDcuFolders.ps1</script>
      <scriptsDirectory>$(CCNetDir)\server\scripts\</scriptsDirectory>
      <buildArgs>"$(SourcesDir)\$(SourcesSubDir)\$(ProjectSubDir)"</buildArgs>
      <successExitCodes>0</successExitCodes>
      <buildTimeoutSeconds>600</buildTimeoutSeconds>
      <description>Cleanup de $(ExeName)$(PlatformSuffix) - $(ProjectSubDir)\$(ProjectName).$(Extension)</description>
    </powershell>

This works just fine

Updated by Ruben Willems over 3 years ago

  • Target version changed from 1.8.5 to 1.9

Also available in: Atom PDF