PowerShell解压错误

6

我从互联网上得到了以下脚本,当我尝试运行它时,出现了错误。

#script
#unzip folder

$shell_app = new-object -com shell.application
$filename = "test.zip"
$zip_file = $shell_app.namespace("C:\temp\$filename")

#set the destination directory for the extracts
$destination = $shell_app.namespace("C:\temp\zipfiles")

#unzip the file
$destination.Copyhere($zip_file.items())

--- 错误消息

    You cannot call a method on a null-valued expression.
At line:1 char:22
+ $destination.Copyhere <<<< ($zip_file.items())
    + CategoryInfo          : InvalidOperation: (Copyhere:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

3
请检查$destination,它很可能是空的,例如$destination -eq $null返回True。 - Keith Hill
1个回答

8

在设置$destination变量之前,您需要创建"c:\temp\zipfiles"文件夹。这是一种马虎的方法,但它可以完成工作 :)

#script
#unzip folder

$shell_app = new-object -com shell.application
$filename = "test.zip"
$zip_file = $shell_app.namespace("C:\temp\$filename")

#set the destination directory for the extracts
if (!(Test-Path "C:\temp\zipfiles\")) { 
    mkdir C:\temp\zipfiles
}
$destination = $shell_app.namespace("C:\temp\zipfiles")

#unzip the file
$destination.Copyhere($zip_file.items())

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接