-ErrorAction SilentlyContinue 不工作

-ErrorAction SilentlyContinue 不工作

我正在使用 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"
            }
        }
    }
}

相關內容