我正在构建Powershell函数,以便可以查询多台计算机的事件日志。香港专业教育学院复制下面的代码,我需要帮助。如果不发送eventID,我希望脚本查找所有事件ID,我该如何实现?
#this would be the parameter part of the script
[String]$ComputerName = $env:COMPUTERNAME#Current computer
[String[]]$EventLogNames=@("Application","System")#Main eventlogs
[int[]]$EventIds = 1 #Event Ids
[System.DateTime[]]$EventStartDate = (((Get-Date).addDays(-2)).date)#date 10 days ago
[System.DateTime[]]$EventEndTime = (Get-Date)#date 10 days ago
#This fits in the process section
$EventCritea = @{logname = $EventLogNames; StartTime=$EventStartDate; EndTime=$EventEndTime; ID=$EventIds}
Get-WinEvent -ComputerName $ComputerName -FilterHashTable $EventCritea -ErrorAction SilentlyContinue
最佳答案
首先,您的其他question修复了$EventStartDate
和$EventEndTime
的类型。
对于此问题:逐步构建哈希表:
$filter = @{logname = $EventLogNames; StartTime=$EventStartDate; EndTime=$EventEndTime}
if ($EventIds -ne $null -and $EventIds.Length -gt 0) {
$filter.ID=$EventIds
}
Get-WinEvent -ComputerName $ComputerName -FilterHashTable $filter #...