shell 命名空间上的 Powershell ParentContainsErrorRecordException

问题描述 投票:0回答:2

我正在尝试编写一个解压缩 powershell 脚本,但我遇到了

$shell.NameSpace()
函数的问题。

function unzip ($sourceFile, $destination){
    $shell = new-object -com shell.application
    $zip = $shell.NameSpace($sourceFile)
    foreach($item in $zip.items()){
        $shell.Namespace($destination).copyhere($item) #Error here
    }
}

unzip "$PWD\folder.zip" $PWD

当我运行此命令时,我在第二次

$shell.NameSpace()
调用时收到错误。

You cannot call a method on a null-valued expression.
At C:\scriptDir\unzipScript.ps1:9 char:6
+         $shell.NameSpace($destination).copyhere($item)
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : InvokeMethodOnNull

我不明白的是为什么这在“第二个”调用中失败了。显然 PWD 存在,并且是基于第一个参数的目录。

    

shell powershell unzip
2个回答
3
投票

unzip "$PWD\folder.zip" $PWD.Path

作为替代方案,您可以使用 .Net System.IO.Compression.ZipFile 类。 
这是一个样本


0
投票

(my_env) PS C:\Users\Sammy> nano hello.py
  • ~~
  • 
    
  • 表达式或语句中出现意外的标记“PS”。 + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullQualifiedErrorId:意外令牌

© www.soinside.com 2019 - 2024. All rights reserved.