Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support executing Powershell scripts in Exec task #11201

Open
bstordrup opened this issue Dec 30, 2024 · 2 comments
Open

Support executing Powershell scripts in Exec task #11201

bstordrup opened this issue Dec 30, 2024 · 2 comments

Comments

@bstordrup
Copy link

Summary

Extend the Exec task to support executing Powershell scripts either using pwsh.exe or powershell.exe

Background and Motivation

Currently, the Exec task is hardcoded to use cmd.exe on Windows, and specifying ToolPath and ToolExe parameters to Exec task having them point to for example pwsh.exe does not help, because there is some internal processing and adding command line parameters specific to cmd.exe

Having support for Powershell scripts in Exec task adds a high level of flexibility to PreBuildEvent and PostBuildEvent as Powershell has a log more features than cmd.exe and batch scripts.

Proposed Feature

Supporting Powershell as command prcoessor requires some changes in the Exec task class. These are centered around the places where the command tool is evaluated, where the potential batch file is written and the execution of the command is hanndled. As far as I can see, it will only be in this class, changes are required.

To avoid changing designers for PreBuildEvent and PostBuildEvent, an option could be to specify the wanted command processor in a comment line first in the script. Then based on this comment line, the Exec class resolves the command processor.

The comment could be something like

#!shell:<processor>

where <processor> would be either cmd, pwsh or powershell. If no specified, cmd would be default as today.

If pwsh is not installed, there are two options:

  1. Fallback to powershell
  2. Fail the task

Alternative Designs

No response

@YuliiaKovalova
Copy link
Member

Hi @bstordrup,

Thank you for the proposed feature.
It's already partially supported by https://github.com/dotnet/msbuild/blob/main/src/Utilities/ToolTask.cs#L61 class.
Please check the example that invokes power shell script: https://github.com/dotnet/msbuild/blob/main/src/MSBuild.Bootstrap.Utils/Tasks/InstallDotNetCoreTask.cs.

@bstordrup
Copy link
Author

Hi @YuliiaKovalova,

The example you mention overrides the ToolName property on the ToolTask to be powershell.exe.

But my point is not generating a Task instance for this - the point is to allow you to use a PowerShell script directly in for example the PreBuildEvent.

Try for example to put the following in the PreBuildEvent field in the Project properties page:

Get-ChildItems $(TargetDir)

and then build the project.

When I tried, I got failing build.

The contents of the PreBuildEvent field is in the project file (for Sdk style projects) transformed into a Target containing an Exec task. And the Exec task defaults to cmd.exe for Windows or sh for non-Windows.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants