使用变量作为启动进程的文件路径参数

发布于 2024-10-07 07:22:58 字数 597 浏览 0 评论 0原文

我想运行一个 .exe,它可能位于多个位置。

$runpath = "$servicepackfolder\SQLServer2008SP1-KB968369-IA64-ENU.exe"

Start-Process -FilePath $runpath -arg "/x:.\$buildfolder\PCU" 

或者通过这种方式指定工作目录:

Start-Process 'SQLServer2008SP1-KB968369-IA64-ENU.exe' -WorkingDirectory $servicepackfolder -arg "/x:.\$buildfolder\PCU"

但变量似乎没有被解释为字符串。

Start-Process:该命令不能 由于错误而执行:系统 找不到指定的文件。

我位于正确的目录中,如果我从 $runpath 变量获取输出并将其替换为 Start-Process 调用中的变量,我会得到预期的行为。

这行得通吗,还是我对这些路径进行了硬编码。尝试自动化 SQL 2008 的滑流构建过程。

I'd like run a .exe which could be in a number of locations.

$runpath = "$servicepackfolder\SQLServer2008SP1-KB968369-IA64-ENU.exe"

Start-Process -FilePath $runpath -arg "/x:.\$buildfolder\PCU" 

Or this way, specifying the WorkingDirectory:

Start-Process 'SQLServer2008SP1-KB968369-IA64-ENU.exe' -WorkingDirectory $servicepackfolder -arg "/x:.\$buildfolder\PCU"

But it seems the variables are not being interpreted as strings.

Start-Process : This command cannot be
executed due to the error: The system
cannot find the file specified.

I am in the correct directory and if I take the output from the $runpath variable and substitute it for the variable in the Start-Process call, I get the expected behavior.

Will this work, or am I stuck hardcoding these paths. Trying to automate the slipstream build process for SQL 2008.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情场扛把子 2024-10-14 07:22:58

我可以复制您看到的行为如果我添加-NoNewWindow,但如果我不指定该参数,它会按我的测试用例的预期工作:

start-process sizeof.exe  -WorkingDirectory C:\temp -ArgumentList 1

新窗口闪烁并消失,但我可以看到它正在运行我的临时目录中指定的 exe。

I can duplicate the behavior you see if I add -NoNewWindow but if I don't specify that parameter it works as expected for my test case:

start-process sizeof.exe  -WorkingDirectory C:\temp -ArgumentList 1

The new window flashes up and goes away but I can see it is running the specified exe from my temp dir.

清君侧 2024-10-14 07:22:58

迟到总比不到好,但是当遇到同样的问题时,我找到了解决方法,不确定它是否被归类为错误 -

Powershell 并不总是处理存储的字符串中未转义的反斜杠或引号在由字符串处理创建的变量 / 中,对于 -FilePath 来说一切都很好,因此对于您的行:

$runpath = "$servicepackfolder\SQLServer2008SP1-KB968369-IA64-ENU.exe"

在使用 $runpath 之前尝试以下操作(或等效操作):

$cleanpath = $runpath.replace("\","\\").replace('"',"")

.replace("\","\\").replace('" ',"") 转义斜杠并消除字符串处理和传递引入的引号,这似乎解决了这个问题(在某些情况下),

我想这对你来说有点晚了,但希望这可以帮助其他人搜索这个问题。

Better late than never, but I've found a workaround for this when having the same issue, not sure if it is classed as a bug or not -

Powershell doesn't always handle un-escaped backslashes or quotes in the strings that are stored in a variable / created by string processing all that well for -FilePath, so for your line:

$runpath = "$servicepackfolder\SQLServer2008SP1-KB968369-IA64-ENU.exe"

Try the following (or equivalent) before using $runpath:

$cleanpath = $runpath.replace("\","\\").replace('"',"")

The .replace("\","\\").replace('"',"") escapes the slashes and eliminates the quotes that the string handling and passing introduce, which seems to clear this issue (for some cases).

Bit late for you I imagine but hopefully this helps other people googling for this one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文