Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Powershell 4中压缩和解压缩文件_Powershell_Zip_Unzip_Powershell 4.0 - Fatal编程技术网

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

在Powershell 4中压缩和解压缩文件,powershell,zip,unzip,powershell-4.0,Powershell,Zip,Unzip,Powershell 4.0,我使用的是Windows Server 2012 R2(64位)。我有powershell版本4可用。我正在尝试压缩和解压文件。当我尝试写入Zip命令时,它会抛出以下错误: Write Zip:术语“Write Zip”不能识别为cmdlet、函数、脚本文件或文件的名称 节目。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试 我该怎么做才能修好它?我需要在服务器中安装zip/winrar吗?或者是否有其他命令执行压缩/解压文件?如果您可以升级到PowerShell V5(),则它

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

Write Zip:术语“Write Zip”不能识别为cmdlet、函数、脚本文件或文件的名称 节目。请检查名称的拼写,或者如果包含路径,请验证路径是否正确,然后重试


我该怎么做才能修好它?我需要在服务器中安装zip/winrar吗?或者是否有其他命令执行压缩/解压文件?

如果您可以升级到PowerShell V5(),则它会以本机方式保存这些文件

对于PowerShell版本4,您可以使用此搜索。这也可以满足您的需求:

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

install-module -name <module name>
安装模块-名称
  • powershellgallery.com是一个免费上传网站。在运行模块之前,请检查并理解模块
希望这有帮助。
谢谢,蒂姆。

应该在PS4下工作。请参见
添加邮政编码>和
新邮政编码>功能

[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
似乎是需要单独安装才能使用的一部分

但是,如果您只想从文件夹创建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
变量中的定义创建存档。注意,类是在.NET Framework 4.5中引入的。您可以使用自定义powershell对象
新建对象-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“核心”版本。如果可能,升级到powershell 5并使用
扩展存档
,因为它更简单

写入Zip安装可能执行不正确。对环境参数PSModulePath进行不正确的手动编辑可能会导致:

错误(原始)值:

良好的值(修复了问题):


我需要压缩一个文件夹。此文件夹有许多子文件夹和文件。有人能告诉我如何在powershell和windows命令行(cmd)中压缩它吗?嗨,Tim,非常感谢您的回复。但当我执行“Install Module”命令时,它抛出与“Write Zip”命令相同的错误,即无法识别此cmd。嗨,Raji,对不起。刚刚检查,您需要从poweshellgallery.com安装PowerShell V5。或者,使用此URL中列出的MSI安装程序。这准确地回答了这个问题,并且运行良好,无需升级或安装任何其他软件。回答得很好。
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\