'Invoke-Expression'删除双引号

时间:2011-11-14 21:21:24

标签: powershell

我有一个包含ZIP文件的目录结构,我想用脚本提取它们。

到目前为止我的脚本:

$7zexe = "c:\bin\7za.exe"
$arg = ""
Get-ChildItem -Recurse -Include *.zip |
ForEach-Object { $argout = $_.Directory.Name
  $arg = " e `"$_`" -o`"$argout`""
  $cmdline = $7zexe, $arg -join ""
  $cmdline
  Invoke-Expression -command  "$cmdline "
}

但是我收到以下错误

c:\bin\7za.exe e "E:\tmp\the folder\the sub folder\my_big_file.zip" -o"the sub folder"
Invoke-Expression : The string starting:
At line:1 char:86
 + c:\bin\7za.exe e "E:\tmp\the folder\the sub folder\my_big_file.zip" -o"the sub folder <<<< "
 is missing the terminator: ".
At x:\mydocs\testscript.ps1:9 char:18
+ Invoke-Expression <<<<  -command  "$cmdline "
    + CategoryInfo          : ParserError: ( :String) [Invoke-Expression], IncompleteParseException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString,Microsoft.PowerShell.Commands.InvokeExpressionCommand

不知何故,看起来PowerShell正在放弃我的结束"

我尝试使用&运算符,我也尝试用数组替换参数:

[Array]$arg = "e ", "`"$_`""," -o`"$argout`""

1 个答案:

答案 0 :(得分:3)

尝试将-o放在引号内:

$7zexe = "c:\bin\7za.exe"
$arg = ""
Get-ChildItem -recurse -Include *.zip | 
ForEach-Object { $argout = $_.Directory.FullName
  write-host -ForegroundColor Green $argout
  $arg = " e `"$_`" `"-o$argout`""
  $cmdline = $7zexe, $arg -join ""
  $cmdline
  Invoke-Expression -command  "$cmdline "
}

旁注:要确保深层嵌套的zip文件的内容解压缩到正确的目录,我相信您应该使用包含目录的fullname属性。