我写了这个Power
Shell脚本,该脚本应该在特定日期范围内对所有日志文件进行归档.
$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.
我该如何让它工作?
您可以手动将
.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"
或在一个32位盒子上:
Add-Type -Path "C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.IO.Compression.FileSystem.dll"
这假定您的系统上的.NET 4.5安装OK,System.IO.Compression.FileSystem.dll实际上存在.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。