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ć?
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). –
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
@bincob dodaj komentarz do odpowiedzi. To powinna być zaakceptowana odpowiedź. –