使用变量作为启动进程的文件路径参数
我想运行一个 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以复制您看到的行为如果我添加
-NoNewWindow
,但如果我不指定该参数,它会按我的测试用例的预期工作:新窗口闪烁并消失,但我可以看到它正在运行我的临时目录中指定的 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:The new window flashes up and goes away but I can see it is running the specified exe from my temp dir.
迟到总比不到好,但是当遇到同样的问题时,我找到了解决方法,不确定它是否被归类为错误 -
Powershell 并不总是处理存储的字符串中未转义的反斜杠或引号在由字符串处理创建的变量 / 中,对于 -FilePath 来说一切都很好,因此对于您的行:
在使用 $runpath 之前尝试以下操作(或等效操作):
.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:
Try the following (or equivalent) before using $runpath:
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.