Author Posts

March 22, 2018 at 4:42 pm

Hello people!

Here is my simple code:

Get-EventLog -LogName System -EntryType Error -Newest 1 -ComputerName (Get-ADComputer -Filter *).name

It works untill it meets machine that does not respond then it stops with an error:

Get-EventLog : Не найден сетевой путь.
строка:1 знак:1
+ Get-EventLog -LogName System -EntryType Error -Newest 1 -ComputerName ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-EventLog], IOException
    + FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.GetEventLogCommand

Parameter "ErrorAction" doesn't work in this case. Is there any simple method to make it work without creating a multistring script?

March 22, 2018 at 4:44 pm

You can try adding -ErrorAction Continue, but it's possible that the way the command is written won't allow you to do what you want in a one-liner. In that case, you'd get the computers, ForEach through them, and do one at a time. The way when one fails, you'll just loop around and try the next one. That's actually a much more common pattern since it would let you capture errors and, for example, log failures for later retry. "Learn PowerShell Scripting in a Month of Lunches" uses that pattern extensively in its running examples.

March 22, 2018 at 4:50 pm

Thank you very much for your answer so fast. But I ask if it is possible to avoid ForEach, Cycles etc and just make it in one line simple code.

March 22, 2018 at 4:51 pm

You're already doing a ForEach inside Get-EventLog, you just don't see it. And no, there's not going to be another way. Well, I mean, you could use Invoke-Command or something, I suppose, but it's going to amount to the same thing unless you can use Remoting. There's always going to be a loop, it's just whether or not you see it or not.

March 23, 2018 at 3:33 pm

This really work! Need WinRM to be enabled on all the machines.

Invoke-Command -ComputerName (Get-ADComputer -Filter * ).name {Get-EventLog -LogName System -Newest 1 -EntryType Error}

March 23, 2018 at 3:34 pm

And if you add -AsJob, it'll run in parallel in the background. Get-Job to see status and Receive-Job to get results. Remoting is wonderful.

March 23, 2018 at 3:36 pm

Thank you! I'll try this