开发者

Use variable for filepath parameter of Start-Process

开发者 https://www.devze.com 2023-01-30 04:25 出处:网络
I\'d like run a .exe which could be in a number of locations. $runpath = \"$servicepackfolder\\SQLServer2008SP1-KB968369-IA64-ENU.exe\"

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 th开发者_运维知识库e slipstream build process for 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:

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消