programing

[시스템] 유형을 찾을 수 없습니다.IO.압축.압축Level]: 이 유형을 포함하는 어셈블리가 로드되었는지 확인합니다.

linuxpc 2023. 8. 4. 22:41
반응형

[시스템] 유형을 찾을 수 없습니다.IO.압축.압축Level]: 이 유형을 포함하는 어셈블리가 로드되었는지 확인합니다.

특정 날짜 범위에서 생성된 모든 로그 파일을 보관하기 위해 이 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);
}

다음 시간까지 작동합니다.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

~하듯이System.IO.Compression의 일부입니다.NET 4.5, 시스템에 설치되어 있지만 여전히 이러한 오류가 발생합니다.윈도우즈 서버 2008 R2에서 PowerShell v2.0을 사용하고 있습니다.

어떻게 하면 이 일을 해낼 수 있을까요?

사용해 보십시오.Add-Type -AssemblyName System.IO.Compression.FileSystem대신.Visual Studio를 설치해야 하는 참조 어셈블리에 종속되지 않고 더 깨끗합니다.

수동으로 를 추가할 수 있습니다.PowerShell 세션의 NET 클래스입니다.

제거한다.[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"

또는 32비트 상자에서:

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

이것은 가정합니다.NET 4.5가 시스템에 정상적으로 설치되었습니다.System.IO.Compression.FileSystem.dll실제로 존재합니다.

또한 $pshome exe.config 파일도 확인해야 합니다.Powershell ISE에서 로드를 거부한 적이 있습니다.구성 파일에 .이(가) 있었기 때문에 NET 어셈블리입니다.4가 아닌 NET 2.0이 나열되었습니다.

언급URL : https://stackoverflow.com/questions/24059263/unable-to-find-type-system-io-compression-compressionlevel-make-sure-that-the

반응형