首页 > 解决方案 > 在 Visual Studio 中运行将 exe 作为预构建步骤运行的 PowerShell 脚本

问题描述

在我的 Visual Studio Pre-built 命令行中,我有一个新创建的行

Powershell.exe -file "$(SolutionDir)Folder1\MyPowerShell.ps1"

在这个 PowerShell 脚本中,它运行一个带有一些参数的 .exe

.\MyProgram.exe .\SomeFileArg.xml

当我只在本地运行 PowerShell 脚本时,无论是作为./MyPowerShell.ps1命令还是将命令复制粘贴到我的 PowerShell 中,它都可以工作。

但是当我构建我的 Visual Studio 时,这不会发生。我收到错误.\MyProgram.exe.exe : The term '.\MyProgram.exe' is not recognized as the name of a cmdlet, 2> function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the 2> path is correct and try again.如果我在我的 PowerShell 脚本中包含绝对路径,那么它可以工作。

如何通过本地路径实现这一目标?还是有一个变量,所以我不必硬编码绝对路径?

标签: visual-studiopowershell

解决方案


您可以使用以下命令在代码中将本地路径(脚本文件所在的位置)声明为变量:

$PSScriptRoot

https://riptutorial.com/powershell/example/27231/-psscriptroot


推荐阅读