【发布时间】:2021-02-24 11:15:23
【问题描述】:
我需要将备份解决方案应用程序生成的日志文件从 RTF 格式连续转换为纯文本(如制作一个全新的日志,该日志是该日志的精确副本,始终是更新副本)为了使用我从另一个用户那里得到的代码来解析日志文件中的错误(请在下面找到它)。知道如何使用 powershell 处理代码吗?
我拥有的可以搜索文本文件的代码:
$daysBack = 3
$refDate = (Get-Date).AddDays(-$daysBack).Date # set this to midnight
$log = Get-Content -Path 'C:\Users\<User>\Documents\TheLog.log'
# find lines that start with what looks like a date and contains 'Errors:'
# capture the date part in backreference $matches[1] to parse into a real datetime object for comparison with $refDate
$errors = @($log | Where-Object { $_ -match '(\d{2}/\d{2}/\d{4} \d{2}:\d{2}:\d{2}).*Errors:' } |
Where-Object { [datetime]::ParseExact($matches[1], 'dd/MM/yyyy HH:mm:ss', $null) -ge $refDate }).Count
# if $errors not 0
if ($errors) {
$count = if ($errors -eq 1) { "was an error" } else { "were $errors errors" }
"There {0} in your back up solution in the last $daysBack days. Please check your log file." -f $count
}
else {
"There were no errors in backing up your files in the last $daysBack days."
}
我已使用以下日志文件的纯文本文件版本进行了测试:
18/02/2021 08:57:37 - can not access C:\users\<username>\documents\ The network path was not found.
18/02/2021 08:57:37 - End: Documents... copied: 0, Errors: 1
21/02/2021 08:57:37 - can not access C:\users\<username>\documents\ The network path was not found.
21/02/2021 08:57:37 - End: Documents... copied: 0, Errors: 1
22/02/2021 17:27:33 - Begin: Documents=======================================
22/02/2021 17:27:33 - copied Notes.docx from C:\users\<username>\documents\ to D:\users\<username>\documents\
22/02/2021 17:27:33 - End: Documents...Copied 1
22/02/2021 17:27:33 - End: Documents...Copied 1, Deleted: 1
上述代码不适用于 RTF。任何帮助或任何想法如何更改上述代码以适合我的 RTF 格式的日志文件?
使用@THEO 的以下代码 :)
Cannot invoke method. Method invocation is supported only on core types in this language mode.
At line:41 char:13
+ $word.Quit()
+ ~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodInvocationNotSupportedInConstrainedLanguage
Cannot invoke method. Method invocation is supported only on core types in this language mode.
At line:42 char:13
+ $null = [System.Runtime.InteropServices.Marshal]::Release ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodInvocationNotSupportedInConstrainedLanguage
Cannot invoke method. Method invocation is supported only on core types in this language mode.
At line:43 char:13
+ $null = [System.Runtime.InteropServices.Marshal]::Release ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodInvocationNotSupportedInConstrainedLanguage
Cannot invoke method. Method invocation is supported only on core types in this language mode.
At line:44 char:13
+ [System.GC]::Collect()
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodInvocationNotSupportedInConstrainedLanguage
Cannot invoke method. Method invocation is supported only on core types in this language mode.
At line:45 char:13
+ [System.GC]::WaitForPendingFinalizers()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : MethodInvocationNotSupportedInConstrainedLanguage
Cannot set property. Property setting is supported only on core types in this language mode.
At line:17 char:9
+ $word.Visible = $false
+ ~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertySetterNotSupportedInConstrainedLanguage
Get-Content : Cannot bind argument to parameter 'Path' because it is null.
At line:55 char:31
+ $log = Get-Content -Path $plainTextLog
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Get-Content], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetContentC
ommand
【问题讨论】:
标签: powershell powershell-3.0 powershell-remoting