Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Visual studio 如何将参数转义到VS post build事件中运行的PS脚本中_Visual Studio_Powershell_Parameter Passing - Fatal编程技术网

Visual studio 如何将参数转义到VS post build事件中运行的PS脚本中

Visual studio 如何将参数转义到VS post build事件中运行的PS脚本中,visual-studio,powershell,parameter-passing,Visual Studio,Powershell,Parameter Passing,更新1: 在再次仔细查看输出后,我大致算出了它。 通过在右括号和引号之间添加尾随空格,它可以工作: powershell -ExecutionPolicy ByPass -File "$(SolutionDir)\BuildScripts\InjectGitVersion.ps1" "$(ProjectDir) " "$(TargetDir) " 我怀疑PowerShell以某种方式解释了)“ 有没有更优雅的方式来解决这个问题 更新2: 这太奇怪了。我有另一个脚本来进行清理,并在)和“之间留出

更新1:

在再次仔细查看输出后,我大致算出了它。 通过在右括号和引号之间添加尾随空格,它可以工作:

powershell -ExecutionPolicy ByPass -File "$(SolutionDir)\BuildScripts\InjectGitVersion.ps1" "$(ProjectDir) " "$(TargetDir) "
我怀疑PowerShell以某种方式解释了
)“

有没有更优雅的方式来解决这个问题

更新2

这太奇怪了。我有另一个脚本来进行清理,并在
之间留出空间,如下所示:

powershell -ExecutionPolicy ByPass -File "$(SolutionDir)\BuildScripts\InjectGitVersionCleanup.ps1" "$(ProjectDir)"
但是,添加尾随空格将导致失败,因为它在内部将文件名附加到路径,因此路径将不正确

如果有人明白这一点,我很乐意接受这个解释作为正确答案


原件:

我在VisualStudio中有以下预构建命令,我想使用它从Git标记注入版本:

powershell -ExecutionPolicy ByPass -File "$(SolutionDir)\BuildScripts\InjectGitVersion.ps1" $(ProjectDir) $(TargetDir)
如果路径$(SolutionDir)不包含空格(空格也将出现在$(ProjectDir)或$(TargetDir)中,则此方法可以正常工作

当路径$(SolutionDir)确实包含空格时,脚本似乎会按预期启动,但参数传递不正确,我无法确定如何在PS脚本的参数中转义它们

我尝试添加sing“、triple”“和”,这会给出以下结果(每个PS命令都尝试使用不同的方法来转义空格):

args[0]:

D:\VisualStudio

args[1]:

项目\软件\u git\u repo\ProgramEditor\

BS:args[0]:

D:\VisualStudio项目\software\u git\u repo\ProgramEditor“D:\VisualStudio

BS:args[1]:

项目\software\u git\u repo\ProgramEditor\bin\Debug“

BS:args[0]:

“D:\VisualStudio

BS:args[1]:

项目\软件\u git\u repo\ProgramEditor“

BS:args[0]:

'D:\VisualStudio

BS:args[1]:

项目\软件\u git\u repo\ProgramEditor\

做:

echo ProjectDir:
echo $(ProjectDir)
echo TargetDir:
echo $(TargetDir)
我得到:

项目目录:

D:\VisualStudio项目\软件\u git\u repo\ProgramEditor\

TargetDir:

D:\VisualStudio项目\software\u git\u repo\ProgramEditor\bin\Debug\


我注意到的一件事是$(SolutionDir)后面有一个额外的反斜杠。我使用的是VS2017,我认为一直以来$(SolutionDir)都有一个反斜杠。不过我不确定

我的解决方案文件夹中有一个.bat,用于创建json配置文件。我的生成后事件如下所示

call "$(SolutionDir)CreateConfigurationJson.bat" "$(TargetDir)mySettings.json"
{"ProjectName":"$(ProjectName)"}
“$(TargetDir)mySettings.json”
是.bat文件的第一个参数

{“ProjectName”:“$(ProjectName)”}
是第二个参数

不需要转义字符。希望这有帮助

bat文件的代码为:

echo off
set filePath=%1
break>%filePath%
set json=%2
echo %json% >>%filePath%

在带引号的参数中展开VS directory宏时,请注意意外的转义引号


包含路径的脚本参数应该用引号括起来,以避免在路径包含空格时被解释为多个参数。因此,在Visual Studio的生成前/生成后事件命令行中,以下内容似乎是合理的:-

powershell.exe -file "$(SolutionDir)script.ps1" -projectDirectory "$(ProjectDir)"
但是指定目录的文件总是包含一个尾随的反斜杠;因此,当它们被展开时,线条会变成,例如:-

powershell.exe -file "C:\sln path\script.ps1" -projectDirectory "C:\sln path\project\"
…其中,尾随\“是一个转义字符,被解释为参数值的一部分,该参数值作为以下内容传递给脚本:-

C:\sln路径\项目”

解决方案 在我的例子中,修复方法是在要转义的目录宏之后插入一个额外的反斜杠,而不是引号,这将恢复预期的参数值并防止任何后续参数的损坏:-

powershell.exe -file "$(SolutionDir)script.ps1" -projectDirectory "$(ProjectDir)\"
但不是很直观。如果我缺少更明显的内容,请告诉我…

@(…)
是PowerShell的一个示例,“它将一个或多个语句的结果作为数组返回。如果只有一个项,则该数组只有一个成员”。我猜这是一个复制品,带有:。解决方案:使用单引号命名参数。
echo off
set filePath=%1
break>%filePath%
set json=%2
echo %json% >>%filePath%
powershell.exe -file "$(SolutionDir)script.ps1" -projectDirectory "$(ProjectDir)"
powershell.exe -file "C:\sln path\script.ps1" -projectDirectory "C:\sln path\project\"
powershell.exe -file "$(SolutionDir)script.ps1" -projectDirectory "$(ProjectDir)\"