我正在尝试更改Get-EventLogs
用于使用的函数Get-WinEvent
。理想情况下,我希望有一个可以每天运行的函数,只需更改一些变量即可扫描不同的日志。
我运行了基本代码,但是当我将其转换为函数时,我无法将变量传递给哈希表中的 LogName。我尝试了不同的引号组合,但没有运气。
我得到日志不存在的错误,只有“-”出现在错误中。
function Check-Eventlogs-v2 {
Param(
[Parameter(Mandatory=$true)][string]$Type
#,[string]$Type
#,[datetime] $date
,[int32]$eventtype
,[string]$box
)
if ($Type -ne '') {
$servers = Get-Content -LiteralPath "C:\temp\sql_servers3.txt"
$Date = (Get-Date).AddDays(-1)
$log = foreach ($box in $servers) {
Get-WinEvent -Computername $box -LogName = Application -FilterHashTable @{
Logname = $Type;
level = $eventtype;
starttime = $Date
} | Where-Object {
($_.Id -ne "2006" -and
$_.Id -ne "1008" -and
$_.Id -ne "12289" -and
$_Logname -eq $Type)
} | Select-Object @{n='HostName';e={($_.MachineName -split '\.')[0]}}, timecreated, id, message
}
} else {
Write-Warning "'$Type' is not a valid log type."
}
return $log
}
试试这个。从过滤器中删除 LogName,并放置一个 ToString 匹配项。您在 Where-Object 中的 LogName 参考中也有一个错字。
$log = foreach ($box in $servers) {
Get-WinEvent -Computername $box -LogName = Application -FilterHashTable @{
level = $eventtype;
starttime = $Date
} | Where-Object {
($_.Id -ne "2006" -and
$_.Id -ne "1008" -and
$_.Id -ne "12289" -and
$_.Logname.ToString() -eq $Type)
} | Select-Object @{n='HostName';e={($_.MachineName -split '\.')[0]}}, timecreated, id, message
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句