出于某种原因,当我尝试使用安全选项时,它实际上并没有执行 get-eventlog cmdlet,而是将其写入屏幕,就像它被引用一样。其他日志在没有 try catch 块的情况下工作,但每当我使用 try catch 块设置它们时,它们仍然像引用 cmdlet 一样运行。
$eventlogname = Read-Host "Which event log category do wish to view? Enter Application, Security, Setup, System, or Forwarded events"
$lognumber = Read-Host "Enter the number of logs you wish to retrieve"
switch
($eventlogname)
{
Security {
$logseverity = Read-Host "What event severity do you wish to view? Enter Critical, Warning, Error, FailureAudit, SuccessAudit, or Information"
$computernameeventlog = Read-Host "Please enter the hostname to query"
if ($computernameeventlog -eq "localhost" )
{
try
{
{
Get-EventLog -logname $eventlogname -EntryType $logseverity -Newest $lognumber -ErrorAction SilentlyContinue
}
}
catch [System.IO.IOException]
{
Write-Host "The hostname was incorrect or not available."
}
catch [System.InvalidOperationException]
{
Write-Host "The event log does not exist"
}
}
else
{
try{
{
Get-EventLog -logname $eventlogname -EntryType $logseverity -ComputerName $computernameeventlog -Newest $lognumber -ErrorAction SilentlyContinue
}
}
catch [System.IO.IOException]
{
Write-Host "The hostname was incorrect or not available."
}
catch [System.InvalidOperationException]
{
Write-Host "The event log does not exist"
}
}
}
"Forwarded events"
{
$computernamewinevent = Read-Host "Please enter the hostname to query"
$logseverity = Read-Host "What event severity do you wish to view? Enter Critical, Warning, Error or Information"
if ($computernamewinevent -eq "localhost")
{
Get-WinEvent -logname forwardedevents -MaxEvents $lognumber | where {$_.leveldisplayname -contains $logseverity}
}
else
{
Get-WinEvent -logname forwardedevents -MaxEvents $lognumber -ComputerName $computername | where {$_.leveldisplayname -contains $logseverity}
}
}
default
{
$logseverity = Read-Host "What event severity do you wish to view? Enter Critical, Warning, Error or Information"
$computernameeventlog = Read-Host "Please enter the hostname to query"
if ($computernameeventlog -eq "localhost" )
{
Get-EventLog -logname $eventlogname -EntryType $logseverity -Newest $lognumber
}
else
{
Get-EventLog -logname $eventlogname -EntryType $logseverity -ComputerName $computernameeventlog -Newest $lognumber
}
}
}