Invoke executable (w/parameters) from powershell script

寵の児 提交于 2019-12-23 15:28:02

问题


I'm calling a zip utility from powershell and having a difficult time getting its parameters straight. Here's the code:

    if (-not (test-path "C:\Program Files (x86)\7-Zip\7z.exe")) {throw "C:\Program Files (x86)\7-Zip\7z.exe needed"} 
    set-alias sz "C:\Program Files (x86)\7-Zip\7z.exe" 

    $argument_1 = "c:\temp\DeployTemp\"
    $argument_0 = "c:\temp\Release\Web_Feature_2012R10_1_1112.prod.com.zip"

    sz x $argument_0 -o$argument_1

The problem is the 7zip executable call literally extracts to a directory named $argument_1, instead of the actual value stored in the string. I've tried escaping the value in a few ways but without luck. Unfortunately the 7zip "-o" flag can't have a space between it and the output directory...


回答1:


Try something like this:

& "$sz" x $argument_0 "-o$argument_1"

The ampersand tells PowerShell to treat the expression more like CMD.exe would, but still allow for the variable expansion (tokens that start with a $).



来源:https://stackoverflow.com/questions/12356869/invoke-executable-w-parameters-from-powershell-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!