我正在使用Windows Server 2012 R2(64位)操作系统,其中安装了PowerShell版本4。我想要压缩和解压文件。当我尝试运行Write-Zip命令时,会抛出以下错误:
Write-Zip:未将术语“Write-Zip”识别为cmdlet、函数、脚本文件或可操作程序的名称。请检查名称的拼写,或者如果包含路径,请验证路径是否正确并重试。
我该怎么做才能解决这个问题?我需要在服务器上安装zip/winrar吗?还是有其他命令可以用来压缩/解压文件?
我正在使用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
变量的定义创建归档文件。请注意,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“核心”版本。如果可能的话,请升级到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\
如果您可以升级到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>
希望这有所帮助。 谢谢,Tim。
应该在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