-ErrorAction 자동으로계속 작동하지 않음

-ErrorAction 자동으로계속 작동하지 않음

저는 powershell로 작업하고 있습니다. C#에서 스크립트를 호출하고 있습니다. 다른 서버의 로그를 복사하려고 하는데,

foreach($server in $args)
{
    $destination = "\\$server\$path"
    if(!(Test-Path $destination))
        {
            New-Item -ItemType directory -Path $destination
        }
    foreach($log in $logsTaskAgent)
    {
        Write-Host "Backup $log log $server"



        $filename = "{0}{1}_{2}_{3}.evt" -f $destination,$server,$log,$date
        Write-Host "Filename: $filename"

        if($password -and $userName)
        {
            $logFile = Get-WmiObject Win32_NTEventlogFile -ComputerName $server -Credential $credential -ErrorAction SilentlyContinue | where-object { $_.logfilename -eq $log } 
            Write-Output "continue"
            if($logFile)
            {
                $logFile.PSBase.Scope.Options.EnablePrivileges = $true 
                $logFile.backupeventlog($filename) 
            }
            else
            {
                Write-Output "Error on server: $server for log: $log"
            }
        }


    }

이것은 완벽하게 작동하지만 자격 증명에 문제가 있으면 예외가 발생하지만 계속하고 싶습니다. 서버가 많기 때문에 예외를 무시하고 다른 서버에서 무엇이 잘못되었는지 계속 알고 싶습니다. ? 지금까지 나는 -ErrorAction Silentlycontinue가 트릭을 수행해야 한다는 것을 이해했지만 그렇지 않습니다.

편집: 수정

나는 Johan de Haan의 답변과 매우 유사한 결과를 얻었으므로 이를 답변으로 삼겠습니다. 수정된 코드는 다음과 같습니다.

foreach($server in $args)
{

    try
    {
        $destination = "\\$server\$path"
        if(!(Test-Path $destination))
            {
                New-Item -ItemType directory -Path $destination
            }
        foreach($log in $logsTaskAgent)
        {
            Write-Host "Backup $log log $server"



            $filename = "{0}{1}_{2}_{3}.evt" -f $destination,$server,$log,$date
            Write-Host "Filename: $filename"

            if($password -and $userName)
            {
                $logFile = Get-WmiObject Win32_NTEventlogFile -ComputerName $server -Credential $credential -ErrorAction SilentlyContinue | where-object { $_.logfilename -eq $log } 
                Write-Output "continue"
                if($logFile)
                {
                    $logFile.PSBase.Scope.Options.EnablePrivileges = $true 
                    $logFile.backupeventlog($filename) 
                }
                else
                {
                    Write-Output "Error on server: $server for log: $log"
                }
            }


        }
    }
    catch
    {
        Write-Output "Error while retrieving log Object from server $server : $($_.Exception.Message)"
        continue
    }

    $destination = ""
}

답변1

다음 서버를 계속하려면 try/catch를 사용하고 중단을 사용합니다.

foreach($server in $args){
    $destination = "\\$server\$path"
    if(!(Test-Path $destination))
    {
        New-Item -ItemType directory -Path $destination
    }
    foreach($log in $logsTaskAgent)
    {
        Write-Host "Backup $log log $server"

        $filename = "{0}{1}_{2}_{3}.evt" -f $destination,$server,$log,$date
        Write-Host "Filename: $filename"

        if($password -and $userName) {
            try{
                 $logFile = Get-WmiObject Win32_NTEventlogFile -ComputerName $server -Credential $credential -ErrorAction Stop | where-object { $_.logfilename -eq $log } 
            } catch {
                 Write-Warning "Error while retrieving WMI Object: $($_.Exception.Message)"
                 break #this way you break out of the first loop, continuing in the foreach ($server in args loop)
            }

            Write-Output "continue"
            if($logFile) {
                $logFile.PSBase.Scope.Options.EnablePrivileges = $true 
                $logFile.backupeventlog($filename) 
            } else {
                Write-Output "Error on server: $server for log: $log"
            }
        }
    }
}

관련 정보