我尝试通过 Visual Studio Target exec 命令调用 BAT,但当文件路径中有空格时会出错。我尝试用
"
将其括起来,但仍然失败。
错误:
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)
这是我的目标电话:
<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 "$(batPath)" "$(TargetPath)""/>
</Target>
该错误非常清楚地说明了问题所在。该路径中有空格,使 power shell 认为这是一个命令。我该如何解决这个问题?
我无权访问构建服务器来测试它,但按照这篇在构建服务器上执行 Windows PowerShell 脚本一文中编写的说明进行操作,它应该接近于:
<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 "& {
&'$(BatPath)'
'$(TargetPath)'} "" />
</Target>
始终在命令中的每个路径周围使用
"
,以确保路径中的任何空格不会将路径字符串分解为多个参数:
<Exec Command=""%WINDIR%\SysNative\WindowsPowerShell\v1.0\powershell.exe" "$(batPath)" "$(TargetPath)""/>