我正在尝试编写一个解压缩 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
存在,并且是基于第一个参数的目录。