How to pass unreachable machine error

Welcome Forums General PowerShell Q&A How to pass unreachable machine error

This topic contains 7 replies, has 2 voices, and was last updated by

 
Participant
9 months ago.

  • Author
    Posts
  • #96788

    Participant
    Points: 4
    Rank: Member

    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?

  • #96791

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    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.

    • #96794

      Participant
      Points: 4
      Rank: Member

      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.

  • #96800

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    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.

    • #96807

      Participant
      Points: 4
      Rank: Member

      Ok. Thanks!

  • #96938

    Participant
    Points: 4
    Rank: Member

    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}
  • #96941

    Keymaster
    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    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.

    • #96944

      Participant
      Points: 4
      Rank: Member

      Thank you! I'll try this

The topic ‘How to pass unreachable machine error’ is closed to new replies.