Powershell解压缩错误

时间:2013-01-23 22:08:34

标签: powershell

我从互联网上获得了以下脚本,当我试图运行它时给了我一个错误。

#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

1 个答案:

答案 0 :(得分:7)

在设置$ 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())