让我在这里简要介绍一下:
我在 AD 中有几台机器需要经常检查。我是 PowerShell 的新手,正在寻找一种可行的方法来实现它。
需要检查什么?远程系统上的某个文件,我们称之为 File1.log(它的名称在每台机器上都相同)。该文件包含许多行,如下所示:
15.10.2013 23:38:54|FA OK 98.9 美元
我需要知道过去 2 小时或 24 小时内有多少类似上述的条目,具体取决于检查时间。(“FA OK”作为表达式工作正常)并获取最后一行的金额。
我已经设法做到了:使用 $checktime2 = (Get-Date).AddHours(-2).ToString("dd.MM.yyyy HH") [匹配格式]。该脚本只是生成空文件,尽管测试文件中的条目正确......代码看起来很糟糕,但我不知道如何检查不同的时间。在我看来 AddHours() 不接受括号中的变量,或者我只是不知道如何设置它。这里的问题 - Google 建议我 Select-String 可以接受多个不同的值(使用 OR,而不是 AND 逻辑运算符)。我不确定这里出了什么问题以及如何继续。脚本有效且不报告错误。任何关于如何使这个脚本工作或至少清理它的反馈都非常受欢迎。提前致谢!
$title = "Check"
$message = "2 or 24 hours back?"
$checktime0 = (Get-Date).AddHours(0).ToString("dd.MM.yyyy HH")
$checktime1 = (Get-Date).AddHours(-1).ToString("dd.MM.yyyy HH")
$checktime2 = (Get-Date).AddHours(-2).ToString("dd.MM.yyyy HH")
$checktime3 = (Get-Date).AddHours(-3).ToString("dd.MM.yyyy HH")
$checktime4 = (Get-Date).AddHours(-4).ToString("dd.MM.yyyy HH")
$checktime5 = (Get-Date).AddHours(-5).ToString("dd.MM.yyyy HH")
$checktime6 = (Get-Date).AddHours(-6).ToString("dd.MM.yyyy HH")
$checktime7 = (Get-Date).AddHours(-7).ToString("dd.MM.yyyy HH")
$checktime8 = (Get-Date).AddHours(-8).ToString("dd.MM.yyyy HH")
$checktime9 = (Get-Date).AddHours(-9).ToString("dd.MM.yyyy HH")
$checktime10 = (Get-Date).AddHours(-10).ToString("dd.MM.yyyy HH")
$checktime11 = (Get-Date).AddHours(-11).ToString("dd.MM.yyyy HH")
$checktime12 = (Get-Date).AddHours(-12).ToString("dd.MM.yyyy HH")
$checktime13 = (Get-Date).AddHours(-13).ToString("dd.MM.yyyy HH")
$checktime14 = (Get-Date).AddHours(-14).ToString("dd.MM.yyyy HH")
$checktime15 = (Get-Date).AddHours(-15).ToString("dd.MM.yyyy HH")
$checktime16 = (Get-Date).AddHours(-16).ToString("dd.MM.yyyy HH")
$checktime17 = (Get-Date).AddHours(-17).ToString("dd.MM.yyyy HH")
$checktime18 = (Get-Date).AddHours(-18).ToString("dd.MM.yyyy HH")
$checktime19 = (Get-Date).AddHours(-19).ToString("dd.MM.yyyy HH")
$checktime20 = (Get-Date).AddHours(-20).ToString("dd.MM.yyyy HH")
$checktime21 = (Get-Date).AddHours(-21).ToString("dd.MM.yyyy HH")
$checktime22 = (Get-Date).AddHours(-22).ToString("dd.MM.yyyy HH")
$checktime23 = (Get-Date).AddHours(-23).ToString("dd.MM.yyyy HH")
$checktime24 = (Get-Date).AddHours(-24).ToString("dd.MM.yyyy HH")
$2 = New-Object System.Management.Automation.Host.ChoiceDescription "&A", `
"Check for 2 hours back"
$24 = New-Object System.Management.Automation.Host.ChoiceDescription "&B", `
"Check for 24 hours back"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($2, $24)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)
switch ($result){
0 {Get-Content File1.log | Select-String '($checktime2|$checktime1|$checktime0)' | Select-String "FA OK" | measure-object -line | out-file OPTcheck.txt -Append
Get-Content File1.xjou | Select-String '($checktime2|$checktime1|$checktime0)' | Select-String "FA OK" | select -Last 1 | out-file OPTcheck.txt -Append
}
1 {Get-Content File1.log| Select-String '($checktime24|$checktime23|$checktime22|$checktime21|$checktime20|$checktime19|$checktime18|$checktime17|$checktime16|$checktime15|$checktime14|$checktime13|$checktime12|$checktime11|$checktime10|$checktime9|$checktime8|$checktime7|$checktime6|$checktime5|$checktime4|$checktime3|$checktime2|$checktime1|$checktime0)' | Select-String "FA OK" | measure-object -line | out-file OPTcheck.txt -Append
Get-Content File1.log | Select-String $checktime24 | Select-String "FA OK" | select -Last 1 | out-file OPTcheck.txt -Append
}
default {Get-Credential} //used to catch the error in switch
}