2014-06-05 26 views
22

Napisałem ten skrypt PowerShell, który ma tworzyć archiwa wszystkich plików dziennika w określonym zakresie dat.Nie można znaleźć typu [System.IO.Compression.CompressionLevel] upewnij się, że zestaw zawierający ten typ jest załadowany

$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); 
} 

Działa aż przed pętli foreach, ale raz w pętli daje te błędy:

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 

Jak System.IO.Compression jest częścią .NET 4.5, mam zainstalowany w systemie, ale wciąż dostaję te błędy.

Jestem na Windows Server 2008 R2 i PowerShell v2.0.

Jak mogę to sprawić?

Odpowiedz

11

Możesz ręcznie dodać klasę .NET do sesji PowerShell.

Usuń [Reflection.Assembly]::LoadWithPartialName("System.IO.Compression.FileSystem"); ze skryptu i dodać następujące na samej górze:

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

lub na polu 32-bitowe:

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

ta zakłada, .NET 4.5 zainstalowany w systemie OK i System.IO.Compression.FileSystem.dll faktycznie istnieje.

+0

To jest dla PowerShell v2, ale dla PowerShell v3 lub późniejszego [LoadWithPartialName jest rzekomo przestarzałe] (http://stackoverflow.com/questions/12923074/how-to-load-assemblies-in-powershell/12924252#12924252). –

+14

Spróbuj zamiast tego użyć 'Add-Type -AssemblyName System.IO.Compression.FileSystem'. Jest czystszy i nie zależy od zespołów referencyjnych, które wymagają instalacji Visual Studio. – bincob

+1

@bincob dodaj komentarz do odpowiedzi. To powinna być zaakceptowana odpowiedź. –

18

Spróbuj użyć zamiast tego Add-Type -AssemblyName System.IO.Compression.FileSystem. Jest czystszy i nie zależy od zespołów referencyjnych, które wymagają instalacji Visual Studio.

+0

Świetna odpowiedź. Nie zależy również od ścieżek plików. – majkinetor

0

Sprawdź także plik $ pshome exe.config. Wystąpił problem, gdy komponent Powershell ISE odmówił załadowania zestawu .NET, ponieważ plik konfiguracyjny zawierał .NET 2.0 zamiast 4.