I am having some problems running a powershell script in a targets file that is being included into some projects.
The MSBuild version is 16.11.0.36601
The following is the relevant stuff from the target file:
<PropertyGroup>
<ScriptLocation>
'$(SolutionDir)subdir\script.ps1'
</ScriptLocation>
...
</PropertyGroup>
<Exec Command="powershell -noprofile -NonInteractive -executionpolicy Unrestricted -File $(ScriptLocation)" />
My problem occurs when the property is expanded. For some reason it appears on a new line, this makes the command fail.
This is the Output:
0>powershell -noprofile -NonInteractive -executionpolicy Unrestricted -File
'C:\code\app\subdir\script.ps1'
0>The command cannot be run because the File parameter requires a file path. Supply a path for the File parameter and then try the command again.
and then script.ps1 is opened in my default editor.
So essentially it's running powershell -noprofile -NonInteractive -executionpolicy Unrestricted -File and then continuing and trying to run the next line, i.e. 'C:\code\app\subdir\script.ps1'
I can't make this work at all - even when I replace it with the documented method here: https://learn.microsoft.com/en-us/aspnet/web-forms/overview/deployment/advanced-enterprise-web-deployment/running-windows-powershell-scripts-from-msbuild-project-files#executing-a-windows-powershell-script-on-the-build-server
When I do that - the following happens:
0>powershell -noprofile -NonInteractive -executionpolicy Unrestricted -Command "&{ &'
C:\code\app\subdir\script.ps1
' } "
0>The string is missing the terminator: '.
0> + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
0> + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString
0>
0>''' is not recognized as an internal or external command
...
Is there a way to tell MSBuild NOT to expand the property on a new line? If not, Is there a way I can wrap things so this doesn't happen?
Rewriting the script in batch or equivalent is not really an option.