无法找到类型[System.IO.Compression.CompressionLevel],请确保已加载包含此类型的程序集

时间:2022-05-18 22:05:10

I have written this PowerShell script which is supposed to make archives of all log files under certain date range.

我编写了这个PowerShell脚本,该脚本应该在特定日期范围内存档所有日志文件。

$currentDate = Get-Date;
$currentDate | Get-Member -Membertype Method Add;
$daysBefore = -1;
$archiveTillDate = $currentDate.AddDays($daysBefore);

$sourcePath = 'C:\LOGS';
$destPath='C:\LogArchieve\'+$archiveTillDate.Day+$archiveTillDate.Month+$archiveTillDate.Year+'.zip';

foreach( $item in (Get-ChildItem $sourcePath | Where-Object { $_.CreationTime -le $archiveTillDate }) )
{
    [Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem");
    $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal;
    [System.IO.Compression.ZipFile]::CreateFromDirectory($sourcePath,$destPath, $compressionLevel, $false);
}

It works till before the foreach loop, but once in a loop it gives these errors:

它一直工作到foreach循环之前,但是在循环中它会产生以下错误:

Unable to find type [System.IO.Compression.CompressionLevel]: make sure that the assembly containing this type is loaded.
At line:4 char:65
+ $compressionLevel = [System.IO.Compression.CompressionLevel] <<<< ::Optimal;
+ CategoryInfo          : InvalidOperation: (System.IO.Compression.CompressionLevel:String) [], RuntimeException
+ FullyQualifiedErrorId : TypeNotFound

As System.IO.Compression is a part of .NET 4.5, I have installed it on the system, but still I get these errors.

由于System.IO.Compression是.NET 4.5的一部分,我已将其安装在系统上,但我仍然遇到这些错误。

I am on Windows Server 2008 R2 and PowerShell v2.0.

我在Windows Server 2008 R2和PowerShell v2.0上。

How can I make it work?

我怎样才能使它工作?

3 个解决方案

#1


13  

You can manually add a .NET class to your PowerShell session.

您可以手动将.NET类添加到PowerShell会话中。

Remove [Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem"); from your script and add the following at the very top:

删除[Reflection.Assembly] :: LoadWithPartialName(“System.IO.Compression.FileSystem”);从您的脚本中添加以下内容:

Add-Type -Path "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.IO.Compression.FileSystem.dll"

Or on a 32-bit box:

或者在32位盒子上:

Add-Type -Path "C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.IO.Compression.FileSystem.dll"

This presumes .NET 4.5 installed OK on your system and System.IO.Compression.FileSystem.dll actually exists.

这假设您的系统上安装了.NET 4.5,并且System.IO.Compression.FileSystem.dll确实存在。

#2


21  

Try using Add-Type -AssemblyName System.IO.Compression.FileSystem instead. It is cleaner and does not have a dependency on the reference assemblies which need an installation of Visual Studio.

请尝试使用Add-Type -AssemblyName System.IO.Compression.FileSystem。它更干净,并且不依赖于需要安装Visual Studio的引用程序集。

#3


0  

Also, make sure to check your $pshome exe.config file. I had an issue once where Powershell ISE refused to load a .NET assembly, because the config file had .NET 2.0 listed instead of 4.

另外,请务必检查$ pshome exe.config文件。我有一个问题,Powershell ISE拒绝加载.NET程序集,因为配置文件列出了.NET 2.0而不是4。

#1


13  

You can manually add a .NET class to your PowerShell session.

您可以手动将.NET类添加到PowerShell会话中。

Remove [Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem"); from your script and add the following at the very top:

删除[Reflection.Assembly] :: LoadWithPartialName(“System.IO.Compression.FileSystem”);从您的脚本中添加以下内容:

Add-Type -Path "C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.IO.Compression.FileSystem.dll"

Or on a 32-bit box:

或者在32位盒子上:

Add-Type -Path "C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.IO.Compression.FileSystem.dll"

This presumes .NET 4.5 installed OK on your system and System.IO.Compression.FileSystem.dll actually exists.

这假设您的系统上安装了.NET 4.5,并且System.IO.Compression.FileSystem.dll确实存在。

#2


21  

Try using Add-Type -AssemblyName System.IO.Compression.FileSystem instead. It is cleaner and does not have a dependency on the reference assemblies which need an installation of Visual Studio.

请尝试使用Add-Type -AssemblyName System.IO.Compression.FileSystem。它更干净,并且不依赖于需要安装Visual Studio的引用程序集。

#3


0  

Also, make sure to check your $pshome exe.config file. I had an issue once where Powershell ISE refused to load a .NET assembly, because the config file had .NET 2.0 listed instead of 4.

另外,请务必检查$ pshome exe.config文件。我有一个问题,Powershell ISE拒绝加载.NET程序集,因为配置文件列出了.NET 2.0而不是4。