在 Powershell 4 中压缩和解压缩文件

Zip and Unzip File in Powershell 4

我正在使用 Windows Server 2012 R2(64 位)。我有 powershell 版本 4 可用。我正在尝试压缩和解压缩文件。当我尝试 Write-Zip 命令时,它抛出以下错误:

Write-Zip : 术语 'Write-Zip' 未被识别为 cmdlet、函数、脚本文件或可操作文件的名称 程序。检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试。

我应该怎么做才能解决它?我需要在服务器上安装 zip/winrar 吗?或者是否有任何其他命令执行 zip/unzip 个文件?

如果您可以升级到 PowerShell V5 (https://www.microsoft.com/en-us/download/details.aspx?id=50395), it has them natively. 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. This also looks to do what you are looking for: https://www.powershellgallery.com/packages/Microsoft.PowerShell.Archive/1.0.1.0

要安装模块,您需要输入:

install-module -name <module name>
  • powershellgallery.com 是一个免费上传的网站。请在运行之前检查并理解模块。

希望这对您有所帮助。 谢谢,蒂姆。

应该在 PS4 下工作。参见Add-ZipNew-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 似乎是 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 class ZipFile 中的 CreateFromDirectory 方法。它从位于 $source 文件夹内的文件创建一个 zip 存档,并创建一个在 $archive 变量中定义的存档。请注意,ZipFile class 是在 .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)

信用来源https://github.com/hashicorp/best-practices/blob/master/packer/scripts/windows/install_windows_updates.ps1#L12-L22

这不适用于 windows 'core' 版本。如果可能,请升级到 powershell 5 并使用 Expand Archive,因为它更简单。

可能未正确执行 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\