visual-studiocommandtargetcsprojmsbuild-target

Calling a command containing path with whitespaces using target 'Exec' in *.csproj files


I am trying to call a BAT via a Visual Studio Target exec command but it errors out when there are spaces in the file path. I tried enclosing it with a " but it still fails.

Error:

1>Task "CreateProperty" (TaskId:58)
1>  Task Parameter:Value=C:\Users\ksobon\source\repos\HOK-Revit-Addins\HOK Beta Tools\src\HOK.BetaToolsManager\..\..\..\_postBuild\codeSigning.bat (TaskId:58)
1>  Output Property: batPath=C:\Users\ksobon\source\repos\HOK-Revit-Addins\HOK Beta Tools\src\HOK.BetaToolsManager\..\..\..\_postBuild\codeSigning.bat (TaskId:58)
1>Done executing task "CreateProperty". (TaskId:58)
1>Using "Exec" task from assembly "Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a".
1>Task "Exec" (TaskId:59)
1>  Task Parameter:Command=%WINDIR%\SysNative\WindowsPowerShell\v1.0\powershell.exe "C:\Users\ksobon\source\repos\HOK-Revit-Addins\HOK Beta Tools\src\HOK.BetaToolsManager\..\..\..\_postBuild\codeSigning.bat" "C:\Users\ksobon\source\repos\HOK-Revit-Addins\HOK Beta Tools\src\HOK.BetaToolsManager\HOK.BetaToolsManager\bin\2018\HOK.BetaToolsManager.dll" (TaskId:59)
1>  %WINDIR%\SysNative\WindowsPowerShell\v1.0\powershell.exe "C:\Users\ksobon\source\repos\HOK-Revit-Addins\HOK Beta Tools\src\HOK.BetaToolsManager\..\..\..\_postBuild\codeSigning.bat" "C:\Users\ksobon\source\repos\HOK-Revit-Addins\HOK Beta Tools\src\HOK.BetaToolsManager\HOK.BetaToolsManager\bin\2018\HOK.BetaToolsManager.dll" (TaskId:59)
1>  C:\Users\ksobon\source\repos\HOK-Revit-Addins\HOK : The term 'C:\Users\ksobon\source\repos\HOK-Revit-Addins\HOK' is  (TaskId:59)
1>  not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or  (TaskId:59)
1>  if a path was included, verify that the path is correct and try again. (TaskId:59)
1>  At line:1 char:1 (TaskId:59)
1>  + C:\Users\ksobon\source\repos\HOK-Revit-Addins\HOK Beta Tools\src\HOK. ... (TaskId:59)
1>  + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ (TaskId:59)
1>      + CategoryInfo          : ObjectNotFound: (C:\Users\ksobon...evit-Addins\HOK:String) [], CommandNotFoundException (TaskId:59)
1>      + FullyQualifiedErrorId : CommandNotFoundException (TaskId:59)
1>    (TaskId:59)
1>C:\Users\ksobon\source\repos\HOK-Revit-Addins\HOK Beta Tools\src\HOK.BetaToolsManager\HOK.BetaToolsManager\HOK.BetaToolsManager.csproj(332,5): error MSB3073: The command "%WINDIR%\SysNative\WindowsPowerShell\v1.0\powershell.exe "C:\Users\ksobon\source\repos\HOK-Revit-Addins\HOK Beta Tools\src\HOK.BetaToolsManager\..\..\..\_postBuild\codeSigning.bat" "C:\Users\ksobon\source\repos\HOK-Revit-Addins\HOK Beta Tools\src\HOK.BetaToolsManager\HOK.BetaToolsManager\bin\2018\HOK.BetaToolsManager.dll"" exited with code 1.
1>Done executing task "Exec" -- FAILED. (TaskId:59)
1>Done building target "CopyFiles" in project "HOK.BetaToolsManager.csproj" -- FAILED.: (TargetId:82)

Here's my target call:

  <Target Name="CopyFiles" AfterTargets="AfterBuild">
    <CreateProperty Value="$(SolutionDir)..\..\..\_postBuild\codeSigning.bat">
      <Output PropertyName="batPath" TaskParameter="Value"/>
    </CreateProperty>
    <Exec Command="%WINDIR%\SysNative\WindowsPowerShell\v1.0\powershell.exe &quot;$(batPath)&quot; &quot;$(TargetPath)&quot;"/>
  </Target>

The error is pretty clear about what the issue is. The path has spaces in it and makes power shell think that's a command. How do I fix this?


Solution

  • I do not have access to a build server to test it, but following the instructions written in this Executing a Windows PowerShell Script on the Build Server article, it should be close to:

    <Target Name="CopyFiles" AfterTargets="AfterBuild">
      <PropertyGroup>
        <PowerShellExe Condition=" '$(PowerShellExe)'=='' "> 
          %WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe
        </PowerShellExe>
        <BatPath Condition=" '$(BatPath)'=='' ">
          C:\Users\ksobon\source\repos\HOK-Revit-Addins\HOK Beta Tools\src\HOK.BetaToolsManager\..\..\..\_postBuild\codeSigning.bat
        </BatPath>
        <TargetPath Condition=" '$(TargetPath)'=='' ">
          C:\Users\ksobon\source\repos\HOK-Revit-Addins\HOK Beta Tools\src\HOK.BetaToolsManager\HOK.BetaToolsManager\bin\2018\HOK.BetaToolsManager.dll
        </TargetPath>
      </PropertyGroup>
      <Exec Command="$(PowerShellExe) -NonInteractive -executionpolicy Unrestricted 
                     -command &quot;&amp; { 
                              &amp;&apos;$(BatPath)&apos; 
                              &apos;$(TargetPath)&apos;} &quot;" />
    </Target>