2017-06-05 253 views
1

我創造了這個腳本來壓縮某個文件夾:壓縮 - 歸檔錯誤:不支持的格式

$timer = (Get-Date -Format yyy-MM-dd_HH-mm-ss) 
$version = 1.1.2 
$folder = New-Item -Name "test_$vesion_$timer" -ItemType directory 
Copy-Item -Path test1\log -Destination $folder -Recurse 
Compress-Archive -Path $folder -DestinationPath $folder 
Remove-Item $folder -Recurse 

但是當我嘗試它,我得到這個錯誤:

Compress-Archive : 1.1.2_2017-06-05_08-58-19 is not a supported archive file format. .zip is the only supported archive file format. 
+ Compress-Archive -Path $folder -DestinationPath $folder 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidArgument: (1.1.2_2017-06-05_08-58-19:String) [Compress-Archive], IOException 
    + FullyQualifiedErrorId : NotSupportedArchiveFileExtension,Compress-Archive 
+0

你設置'$ version'但隨後使用'$ vesion'(缺少R)下一行,所以你的文件夾將不會被創建。 –

回答

0

你給壓縮文件與文件夾名稱相同,在您的示例中爲test_1.1.2_2017-06-05_08-58-19。問題是由$version變量引入的.字符會使名稱的其餘部分看起來像文件擴展名。如果您給歸檔文件的擴展名不是.zip,則Compress-Archive cmdlet將返回此錯誤。

您應該能夠通過簡單地添加.zip到存檔名稱的末尾來解決這個問題,比如:在

Compress-Archive -Path $folder -DestinationPath "$folder.zip" 
+0

當我做這個改變時,我得到這個:壓縮存檔:無法驗證參數'DestinationPath'上的參數。參數爲空或空。提供一個不爲空或空的參數,然後再次嘗試該命令。 v1.0 \ backup_before_update.ps1:14 char:49 + Compress-Archive -Path $文件夾-DestinationPath $ folder.zip –

+0

@ h.s是否將目標路徑值放在雙引號中? – Nacimota

+0

Compress-Archive -Path $文件夾-DestinationPath「$ folder.zip」我現在沒有erreur,但我的文件夾的名稱仍然只有test_2017-06-05_08-58-19所以視覺沒有添加到文件夾名稱 –