-ErrorAction SilentlyContinuar sin funcionar

-ErrorAction SilentlyContinuar sin funcionar

Estoy trabajando con PowerShell. Estoy llamando a un script desde C#. Estoy intentando copiar registros de diferentes servidores.

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"
            }
        }


    }

Esto funciona perfectamente, pero cuando algo anda mal con las credenciales genera una excepción, pero quiero continuar porque, como dije, tengo muchos servidores, así que solo quiero ignorar la excepción y continuar con los otros servidores, cualquier idea de lo que está mal. ? Hasta donde tengo entendido, -ErrorAction Silentlycontinue debería funcionar, pero no lo es :(

Editar: arreglar

Terminé con algo muy similar a la respuesta de Johan de Haan, así que tomaría eso como la respuesta... aquí está el código modificado:

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 = ""
}

Respuesta1

Haría uso de try/catch y usaría un descanso para continuar con el siguiente servidor:

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"
            }
        }
    }
}

información relacionada