无法使用 Powershell 2.0 压缩文件
Unable to compress a file with Powershell 2.0
Q1。我尝试了几种压缩方法,但 none 它在我的机器上工作。我只能使用外部压缩工具 7z.exe 来压缩文件,但我没有权限在 serverA 中安装 7z.exe 文件,也没有将 powershell 更新到 v5。 目前使用 powershell v2
尝试如下,但 none 有效。那么,还有其他方法可以介绍我压缩文件吗?
- 添加类型 -assembly "system.io.compression.filesystem"
- 压缩存档
- 添加类型-路径 C:\Reference\Draftdoc.docx
- C:\Reference\Images\diagram2.vsd -压缩级别最佳
-DestinationPath C:\Archives\Draft.Zip
Q2。下面是我正在使用 7z.exe 工具的查询(这个 serverB 确实带有 7z.exe)但是我遇到了错误。我想用今天的日期压缩任何文件。
$timestamp = (Get-Date).ToString('yyyy-MM')
$source = "D:\csv\*.csv", "D:\csv2\*.csv"
$target = "D:\CSV2_$timestamp.zip"
zip = "D:\Program Files-Zipz.exe"
#Compressed file
if (-not (test-path zip)) {throw 'zip needed'}
set-alias sz zip
sz a -mx=9 $target $source
{
Get-ChildItem $source | Where{$_.LastWriteTime -gt (Get-Date).AddDays(-1)}
}
注意:两个服务器我也需要压缩文件但是服务器A没有7z,但是服务器B有7z.exe
这应该有效:
Add-Type -Assembly "System.IO.Compression.FileSystem"
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcePath, $destinationZip)
有关加载所需程序集的替代方法,请参阅
您将不得不使用更旧的 Shell.Application
COM Object method。
function Extract-Zip
{
param([string]$zipfilename, [string] $destination)
if(test-path($zipfilename))
{
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
$destinationFolder = $shellApplication.NameSpace($destination)
$destinationFolder.CopyHere($zipPackage.Items())
}
}
请注意,我认为这仅适用于 Windows Vista 或 Server 2008 或更高版本。如果您使用的是 Server 2003——而您不应该使用——那么据我所知,您将不得不使用第三方软件。
不言而喻,但您迫切需要更新您的服务器。我并不是说您需要安装最新的 PowerShell。我是说你明显使用的是 Server 2008 R2 或更早的版本,而这是 2019 年。
根据你的第二个问题,7z安装在ServerB中,这个函数压缩了你想要的文件,这段代码不依赖于你的powershell版本。
function Compress-Items ($ItemsPaths, $dest) {
$Path = "D:\Program Files-Zipz.exe"
$argList = "a -tzip -y `"$dest`""
foreach ($item in $ItemsPaths) {
$argList += " `"$item`""
}
Start-Process -FilePath $Path -ArgumentList $argList
}
$source = (get-childitem -Path "D:\csv", "D:\csv2" -Include "*.csv" -Recurse).FullName
Compress-Items -ItemsPaths $source -dest $destination
备注
我修改了你的 $source
因为这是获取你想要的所有 csv 文件的正确方法。
Q1。我尝试了几种压缩方法,但 none 它在我的机器上工作。我只能使用外部压缩工具 7z.exe 来压缩文件,但我没有权限在 serverA 中安装 7z.exe 文件,也没有将 powershell 更新到 v5。 目前使用 powershell v2 尝试如下,但 none 有效。那么,还有其他方法可以介绍我压缩文件吗?
- 添加类型 -assembly "system.io.compression.filesystem"
- 压缩存档
- 添加类型-路径 C:\Reference\Draftdoc.docx
- C:\Reference\Images\diagram2.vsd -压缩级别最佳
-DestinationPath C:\Archives\Draft.Zip
Q2。下面是我正在使用 7z.exe 工具的查询(这个 serverB 确实带有 7z.exe)但是我遇到了错误。我想用今天的日期压缩任何文件。
$timestamp = (Get-Date).ToString('yyyy-MM')
$source = "D:\csv\*.csv", "D:\csv2\*.csv"
$target = "D:\CSV2_$timestamp.zip"
zip = "D:\Program Files-Zipz.exe"
#Compressed file
if (-not (test-path zip)) {throw 'zip needed'}
set-alias sz zip
sz a -mx=9 $target $source
{
Get-ChildItem $source | Where{$_.LastWriteTime -gt (Get-Date).AddDays(-1)}
}
注意:两个服务器我也需要压缩文件但是服务器A没有7z,但是服务器B有7z.exe
这应该有效:
Add-Type -Assembly "System.IO.Compression.FileSystem"
[System.IO.Compression.ZipFile]::CreateFromDirectory($sourcePath, $destinationZip)
有关加载所需程序集的替代方法,请参阅
您将不得不使用更旧的 Shell.Application
COM Object method。
function Extract-Zip
{
param([string]$zipfilename, [string] $destination)
if(test-path($zipfilename))
{
$shellApplication = new-object -com shell.application
$zipPackage = $shellApplication.NameSpace($zipfilename)
$destinationFolder = $shellApplication.NameSpace($destination)
$destinationFolder.CopyHere($zipPackage.Items())
}
}
请注意,我认为这仅适用于 Windows Vista 或 Server 2008 或更高版本。如果您使用的是 Server 2003——而您不应该使用——那么据我所知,您将不得不使用第三方软件。
不言而喻,但您迫切需要更新您的服务器。我并不是说您需要安装最新的 PowerShell。我是说你明显使用的是 Server 2008 R2 或更早的版本,而这是 2019 年。
根据你的第二个问题,7z安装在ServerB中,这个函数压缩了你想要的文件,这段代码不依赖于你的powershell版本。
function Compress-Items ($ItemsPaths, $dest) {
$Path = "D:\Program Files-Zipz.exe"
$argList = "a -tzip -y `"$dest`""
foreach ($item in $ItemsPaths) {
$argList += " `"$item`""
}
Start-Process -FilePath $Path -ArgumentList $argList
}
$source = (get-childitem -Path "D:\csv", "D:\csv2" -Include "*.csv" -Recurse).FullName
Compress-Items -ItemsPaths $source -dest $destination
备注
我修改了你的 $source
因为这是获取你想要的所有 csv 文件的正确方法。