[英]Zip and Unzip File in Powershell 4
我使用的是 Windows Server 2012 R2(64 位)。 我有可用的 powershell 版本 4。 我正在嘗試壓縮和解壓縮文件。 當我嘗試 Write-Zip 命令時,它會引發以下錯誤:
Write-Zip :術語“Write-Zip”不被識別為 cmdlet、函數、腳本文件或可運行程序的名稱。 檢查名稱的拼寫,或者如果包含路徑,請驗證路徑是否正確,然后重試。
我應該怎么做才能修復它? 我需要在服務器中安裝 zip/winrar 嗎? 或者有沒有其他命令可以壓縮/解壓縮文件?
Write-Zip
似乎是http://pscx.codeplex.com/ 的一部分,需要單獨安裝才能使用它。
但是,如果您只想從文件夾創建 Zip 存檔,則可以運行
$source = "c:\temp\source"
$archive = "c:\temp\archive.zip"
Add-Type -assembly "system.io.compression.filesystem"
[io.compression.zipfile]::CreateFromDirectory($source, $archive)
這利用了 .NET Framework 類ZipFile
的CreateFromDirectory
方法。 它從位於$source
文件夾內的文件創建一個 zip 存檔,並創建一個在$archive
變量中定義的$archive
。 注意, ZipFile類是在 .NET Framework 4.5 中引入的
您可以使用自定義 powershell 對象New-Object -ComObject Shell.Application
並復制帶有標志的文件以解壓縮。
$filePath = "foo.zip"
$shell = New-Object -ComObject Shell.Application
$zipFile = $shell.NameSpace($filePath)
$destinationFolder = $shell.NameSpace("C:\Program Files\WindowsPowerShell\Modules")
$copyFlags = 0x00
$copyFlags += 0x04 # Hide progress dialogs
$copyFlags += 0x10 # Overwrite existing files
$destinationFolder.CopyHere($zipFile.Items(), $copyFlags)
這不適用於 windows 'core' 版本。 如果可能,升級到 powershell 5 並使用Expand Archive
因為它要簡單得多。
如果您可以升級到 PowerShell V5 ( https://www.microsoft.com/en-us/download/details.aspx?id=50395 ),它本身就有它們。 https://richardspowershellblog.wordpress.com/2014/10/25/powershell-5-zip-and-unzip/
對於 PowerShell 版本 4,您可以使用此搜索http://www.powershellgallery.com/items?q=zip&x=0&y=0 。 這也可以滿足您的要求: https : //www.powershellgallery.com/packages/Microsoft.PowerShell.Archive/1.0.1.0
要安裝模塊,您需要鍵入:
install-module -name <module name>
希望這會有所幫助。 謝謝,蒂姆。
應該在 PS4 下工作。 請參閱Add-Zip
和New-Zip
函數
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True)]
[ValidateScript({Test-Path -Path $_ })]
[string]$sourceDirectory,
[Parameter(Mandatory=$True)]
[ValidateScript({-not(Test-Path -Path $_ -PathType Leaf)})]
[string]$destinationFile,
[Parameter(Mandatory=$True)]
[int]$noOlderThanHours
)
function New-Zip
{
param([string]$zipfilename)
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipfilename).IsReadOnly = $false
}
function Add-Zip
{
param([string]$zipfilename)
if(-not (test-path($zipfilename)))
{
set-content $zipfilename ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18))
(dir $zipfilename).IsReadOnly = $false
}
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
foreach($file in $input)
{
$zipPackage.CopyHere($file.FullName)
Start-sleep -milliseconds 500
}
}
$oldest = (Get-Date) - (New-TimeSpan -Hours $noOlderThanHours)
$filesToZip = dir $sourceDirectory -Recurse | Where-Object {$_.lastwritetime -gt $oldest}
Write-Host Going to zip following files
$filesToZip | foreach {Write-Host $_.FullName}
$filesToZip| Add-Zip $destinationFile
Write-Zip 安裝可能未正確執行。 環境參數 PSModulePath 的手動編輯不正確可能會導致:
壞(原始)值:
PSModulePath = %SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\;C:\Program Files\Intel\
物有所值(解決了問題):
PSModulePath = C:\Program Files (x86)\PowerShell Community Extensions\Pscx3\;%SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\;C:\Program Files\Intel\
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.