Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell Try/Catch块实际上并不执行get-eventlog cmdlet_Powershell_Try Catch_Get Eventlog - Fatal编程技术网

Powershell Try/Catch块实际上并不执行get-eventlog cmdlet

Powershell Try/Catch块实际上并不执行get-eventlog cmdlet,powershell,try-catch,get-eventlog,Powershell,Try Catch,Get Eventlog,出于某种原因,当我尝试使用安全选项时,它实际上并不执行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 For

出于某种原因,当我尝试使用安全选项时,它实际上并不执行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
        }
}
}

在代码中,用括号(
{
}
)将命令括起来,如下所示:

{
Get-EventLog -logname $eventlogname -EntryType $logseverity -ComputerName $computernameeventlog -Newest $lognumber -ErrorAction SilentlyContinue 
}

这将生成一个脚本块,它不会执行,而是作为字符串输出。您需要的是移除支架。

如果您要修复Micky提到的各种支架问题,您的捕获块将永远不会运行。在Try块中,必须强制执行终止错误。您强制它完全忽略错误:

#Note the changed error action:
Get-EventLog -logname $eventlogname -EntryType $logseverity -Newest $lognumber -ErrorAction Stop

#Get some help on the topic:
Get-Help about_Try_Catch_Finally
进一步阅读可能会有所帮助:

  • -PowerShell错误处理大全

最后,在旁边,考虑更可读的格式化。一些流行的选择:

Try
{
    #Do something
}
Catch 
{
    #Catch
}

Try {
    #Do something
}
Catch {
    #Catch
}
许多人对这些习俗和其他习俗有强烈的支持和反对情绪,但只要你始终如一,你就应该是好人。我看到了第一个示例中的一些内容,但是在某些情况下,您缩进了第一个花括号,而不是将其与关键字对齐


干杯

这就解决了问题,但当我添加另一个catch块时,它又回来了。你能给我看看新问题的代码吗?还是最好提出一个新问题?我最终彻底清理了它,并重做了每一块。