2

I'm working with powershell I'm calling a script from c#. I'm trying to copy logs from different servers,

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


    }

This works perfectly, but when something is wrong with the credentials it throw an exception but I want to continue because has I said, I have many servers so I just want to ignore the exception and continue with the other servers any idea of what is wrong? As far has I understand -ErrorAction Silentlycontinue should do the trick but it's not :(

Edit: Fix

I ended up with something very similar to the answer of Johan de Haan so I would take that as the answer... here's the code modified:

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 = ""
}
3
  • 2
    If you want to completely suppress all error output, then use a Try/Catch and use -EA Stop, and then do nothing in your Catch block.
    – Ryan Ries
    Jun 11 '15 at 20:01
  • Thanks, I don't want to suppress, I want to continue with the next server, going to update the code so you can see the loop. Jun 11 '15 at 20:06
  • @user1216761, Ryan Riles's suggestion will do what you want if you wrap the offending code in the try/catch. You can also try -ea Ignore, or perhaps the error is already being ignored but it's the where-object that is failing? It would help if you posted the error. Also this code looks incomplete. You reference $userName and $password in the if but never use them, and you use $credential in the cmdlet call, but never define it.
    – briantist
    Jun 12 '15 at 1:38
1

I would make use of a try/catch and use a break to continue with next server:

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"
            }
        }
    }
}
1
  • I'm going to take this as the correct answer, I figure it out yesterday night and end up with something very similar to this, I will update my post, thanks Jun 12 '15 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.