2014-11-05 67 views
1

我嘗試設置一個安裝腳本,它在新安裝的Windows 7 64位系統上設置開發環境。 到目前爲止,我不能依賴像7zip這樣的第三方工具,因爲新用戶在第一天可能沒有安裝軟件的權利。Powershell解壓縮功能 - 奇怪的行爲

通常這不是什麼大事,我有解壓文件的腳本。但昨天我偶然發現了一臺不想「解壓縮」我的文件的電腦。 這裏是一個簡短的測試腳本,我寫來縮小問題:

$shell=new-object -com shell.application 

$curLoc=get-location 
write-host "Current Location is $($curLoc)" 

$curPath=$curLoc.path 
write-host "Current Path is $($curPath)" 

$dest=$shell.namespace($curPath) 
write-host "Destination-Object is $($dest)" 

$zipFiles = get-childitem *.zip 
write-host "zipfiles found:" 
write-host $zipFiles 

foreach ($singleZipFile in $zipFiles) 
{ 
    write-host "Unzipping zipfile $($singleZipFile.fullname)" 

    $zipFolder = $shell.namespace($singleZipFile.fullname) 
    write-host "ZipFolder:" 
    write-host $zipFolder 

    $dest.Copyhere($zipFolder.items()) 
    write-host "Unzipping zipfile $($singleZipFile.fullname) done" 
} 

在我的測試計算機運行沒有問題:

PS F:\testcenter> .\unzip4.ps1 
Current Location is F:\testcenter 
Current Path is F:\testcenter 
Destination-Object is System.__ComObject 
zipfiles found: 
F:\testcenter\test.zip 
Unzipping zipfile F:\testcenter\test.zip 
ZipFolder: 
System.__ComObject 
Unzipping zipfile F:\testcenter\test.zip done 
PS F:\testcenter> 

但在有問題的電腦是否如預期般出現故障:

PS F:\testcenter> .\unzip4.ps1 
Current Location is F:\testcenter 
Current Path is F:\testcenter 
Destination-Object is System.__ComObject 
zipfiles found: 
F:\testcenter\test.zip 
Unzipping zipfile F:\testcenter\test.zip 
Exception calling "NameSpace" with "1" argument(s): "Unknown Error (Except 
ion from HRESULT: 0x80004005 (E_FAIL))" 
At F:\testcenter\unzip4.ps1:20 char:32 
+ $zipFolder = $shell.namespace <<<< ($singleZipFile.fullname) 
    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : ComMethodTargetInvocation 

ZipFolder: 

You cannot call a method on a null-valued expression. 
At F:\testcenter\unzip4.ps1:24 char:34 
+ $dest.Copyhere($zipFolder.items <<<<()) 
    + CategoryInfo   : InvalidOperation: (items:String) [], RuntimeExce 
    ption 
    + FullyQualifiedErrorId : InvokeMethodOnNull 

Unzipping zipfile F:\testcenter\test.zip done 
PS F:\testcenter> 

即使我強迫某些PS版本的使用問題保持不變...

很明顯,問題是將zipfile路徑和名稱作爲參數傳遞給.namespace。 但是爲什麼?我能做些什麼呢? 首先,我認爲這是一個有關權利的問題,並在沒有管理權限的計算機上對其進行測試。但沒有發生錯誤。

回答

0

不完全確定失敗的原因,但是如果您使用的計算機可靠地擁有.Net 4.5,則可以使用腳本的下面更改來代替COM對象。或者,查看我發現.Net方法的源代碼,在Powershell Script Repository.

(由於事實上沒有任何COM對象,因此有些變量現在可能是冗餘的。調整輸出和變量以西裝)

# Load .Net 4.5 Compression File System, suppress output. 
[System.Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem") | Out-Null 

$curLoc=get-location 
write-host "Current Location is $($curLoc)" 

$curPath=$curLoc.path 
write-host "Current Path is $($curPath)" 

$dest = $curPath 
write-host "Destination-Object is $($dest)" 

$zipFiles = get-childitem *.zip 
write-host "Zipfiles found:" 
write-host $zipFiles 

foreach ($singleZipFile in $zipFiles) 
{ 
    write-host "Unzipping zipfile $singleZipFile" 

    $zipFolder = $singleZipFile 
    write-host "ZipFolder:" 
    write-host $zipFolder 
#Call .Net 4.5 Zip Extraction 
    [System.IO.Compression.ZipFile]::ExtractToDirectory("$singleZipFile", "$Dest") 
    write-host "Unzipping zipfile $singleZipFile done" 
} 
+0

感謝您的回答。但它也沒有效果。目前我正在使用自解壓存檔文件... – 2015-01-08 14:37:25

+0

有趣。用.Net方法看到什麼樣的錯誤?它是否與其他錯誤相似? – 2015-01-08 16:57:03

+0

對不起,沒有注意到評論。這幾乎是相同的錯誤......我想這是一個不知何故的Windows安裝/配置。 – 2015-03-18 08:29:50