2013-01-23 44 views
6

Tôi nhận được một tập lệnh sau từ internet, khi tôi cố gắng chạy nó cho tôi một lỗi.Lỗi giải mã 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()) 

--- Thông báo lỗi

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

Kiểm tra điểm đến $, có thể là ví dụ: '$ destination -eq $ null' trả về True. –

Trả lời

7

bạn cần để tạo ra các "c: \ temp \ zipfiles" thư mục trước khi bạn đặt biến $ đích, này loại cách cẩu thả nhưng nó sẽ làm công việc :)

#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()) 
+0

Cảm ơn, nó đã hoạt động. – Lakhae

Các vấn đề liên quan