gpt4 book ai didi

powershell - 无法找到类型 [System.IO. Compression. CompressionLevel] : make sure that the assembly containing this type is loaded

转载 作者:行者123 更新时间:2023-12-02 23:58:12 25 4
gpt4 key购买 nike

我编写了这个 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 的一部分,我已将其安装在系统上,但仍然收到这些错误。我使用的是 Windows Server 2008 R2 并使用 PowerShell v2.0

我怎样才能做到这一点?

最佳答案

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

关于powershell - 无法找到类型 [System.IO. Compression. CompressionLevel] : make sure that the assembly containing this type is loaded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24059263/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com