English 中文(简体)
当呼吁其他方案时,权力变化
原标题:Powershell variable expansion when calling other programs

I have a small problem trying to unzip a file using the 7za command-line utility in Powershell.

I set the $zip_source variable to the zip file s path and the $unzip_destination to the desired output folder.

但是,<条码>7za<>>>的指挥系统使用需要具体表述如下:

7za x -y <zip_file> -o<output_directory>

因此,我目前的呼吁是:

&  7za  x -y "$zip_source" -o$unzip_destination

Due to the fact that there can be no space between -o and the destination it seems that PowerShell will not expand the $unzip_destination variable, whereas $zip_source is expanded.

Currently the program simply extracts all the files into the root of C: in a folder named $unzip_destination. Setting different types of quotes around the variable won t work neither:

-o"$unzip_destination" : still extracts to C:$unzip_destination
-o $unzip_destination  : still extracts to C:$unzip_destination
-o $unzip_destination  : Error: Incorrect command line

是否有办法在指挥之前强行扩大?

最佳回答

努力这样做:

-o $($unzip_destination)

Editor s note: This solution only works with a space after -o (in which case just -o $unzip_destination would do) - if you remove it, the command doesn t work as intended.
This approach is therefore not suitable for appending a variable value directly to an option name, as required by the OP.

问题回答

引证:

&  7za  x -y "$zip_source" "-o$unzip_destination" 

这一工作应当:

&  7za  x -y $zip_source -o${unzip_destination} 




相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

热门标签