2016-11-19 133 views
0

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

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

我应该怎么做才能解决它?我需要在服务器中安装zip/winrar吗?或者有没有其他命令做zip /解压缩文件?

+0

我需要压缩一个文件夹。这个文件夹有很多子文件夹和文件。有人可以告诉我如何在PowerShell中以及在Windows命令行(cmd)中压缩它 – Raji

回答

7

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类ZipFileCreateFromDirectory方法。它从位于$source文件夹内的文件创建一个zip存档,并创建$archive变量中定义的存档。注意,ZipFile类的.NET Framework 4.5

+0

这很有效。非常感谢Kim – Raji

0

如果您可以升级到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> 
  • powershellgallery.com是一个免费的上传网站。运行前请检查并理解模块。

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

+0

您好,非常感谢您的回复。但是当我执行“Install-Module”命令时,它会抛出与“Write-Zip”命令相同的错误,该cmd无法识别。 – Raji

+1

嗨Raji,对不起。刚刚检查过,您需要从poweshellgallery.com安装PowerShell V5。或者,使用此URL中列出的MSI安装程序。 https://msdn.microsoft.com/en-us/powershell/gallery/readme?f=255&MSPPError=-2147217396 –

-1

应该在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 
1

对于Powershell的版本中引入>在内置功能= 5“压缩 - 归档”

$source = "C:\temp\foldertozip" 

If(Test-path $source) 
{ 
    $destFolder = "$($source)_$(get-date -f yyyyMMdd_HHmm)"   
    Compress-Archive -Path $source -DestinationPath "$($destFolder).zip" 
} 
+0

确保文件夹不是空的 –

0

可以使用自定义的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的 '核心' 编辑工作。如果可能的话,升级到PowerShell 5并使用Expand Archive,因为它更简单。