Mav*_*ven 31 compression powershell runtime-error powershell-2.0 .net-assembly
我编写了这个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);
}
Run Code Online (Sandbox Code Playgroud)
它一直工作到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
Run Code Online (Sandbox Code Playgroud)
作为.NET 4.5 System.IO.Compression的一部分,我已经将它安装在系统上,但我仍然遇到这些错误.
我在Windows Server 2008 R2和PowerShell v2.0上.
我怎样才能使它工作?
bin*_*cob 28
请尝试使用Add-Type -AssemblyName System.IO.Compression.FileSystem.它更干净,并且不依赖于需要安装Visual Studio的引用程序集.
Raf*_*Raf 15
您可以手动将.NET类添加到PowerShell会话中.
[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"
Run Code Online (Sandbox Code Playgroud)
或者在32位盒子上:
Add-Type -Path "C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.IO.Compression.FileSystem.dll"
Run Code Online (Sandbox Code Playgroud)
这假设.NET 4.5在您的系统上安装正常并且System.IO.Compression.FileSystem.dll实际存在.
| 归档时间: |
|
| 查看次数: |
51453 次 |
| 最近记录: |