简体   繁体   English

使用变量作为Start-Process的filepath参数

[英]Use variable for filepath parameter of Start-Process

I'd like run a .exe which could be in a number of locations. 我想运行一个.exe,它可以在许多位置。

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

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

Or this way, specifying the WorkingDirectory: 或通过这种方式,指定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. 我在正确的目录中,如果我从$ runpath变量中获取输出并将其替换为Start-Process调用中的变量,则会得到预期的行为。

Will this work, or am I stuck hardcoding these paths. 这行得通吗,还是我坚持对这些路径进行硬编码。 Trying to automate the slipstream build process for SQL 2008. 尝试自动化SQL 2008的补充流构建过程。

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: 如果添加-NoNewWindow我可以复制您看到的行为,但是如果我不指定该参数,它将按我的测试用例预期的那样工作:

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. 新窗口闪烁并消失,但我可以从临时目录中看到它正在运行指定的exe。

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: Powershell并不总是处理存储在变量中的未转义的反斜杠或引号,或者通过对-FilePath进行字符串处理而很好地创建了字符串,因此对于您的行:

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

Try the following (or equivalent) before using $runpath: 在使用$ 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). .replace(“ \\”,“ \\\\”)。replace('“',”“)转义斜线并消除字符串处理和传递引入的引号,这似乎可以解决此问题(在某些情况下)。

Bit late for you I imagine but hopefully this helps other people googling for this one. 我想对您来说有点晚,但是希望这可以帮助其他人使用此工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM