Invoke-webrequest breaking foreach loop

Welcome Forums General PowerShell Q&A Invoke-webrequest breaking foreach loop

Viewing 3 reply threads
  • Author
    • #246000
      Topics: 8
      Replies: 9
      Points: 102
      Rank: Participant

      Hi there people!

      I am struggling with a “break of completing” in my foreach loop that scans websites for DNS information.

      The aparrant reason for my issue, is that i call for an invoke-webrequest besides my resolve-dnsname call.

      It seems, that if my invoke-webrequest cannot complete, then neither will the rest of the code, and i simply get empty outputs on Nameserver, MX records and so on.

      I have tried for quite a while to figure out how to tell powershell to ignore if invoke-webrequest cannot be completed, but ive had no luck so far.

      If i remove the section “$DomainFinal = (Invoke-WebRequest -Uri $DomainName********” the scan works well enough.
      The reason for my invoke-webrequest, is in order to get the “final destination” of the call, to find if the websites redirects somewhere else, which is a function i would really like to keep. the Rmatch function just checks if the final destination matches the searched domain and returns false if not.

      If the invoke-webrequest fails, all i get is “The remote server returned an error: (403) Forbidden. ” and no other output.

      Can anyone help me figure out, how to allow a false return on the invoke-webrequest, while still getting the rest of the available data?

      A picture of an output:
      powershell error
      The second last in the scan is the one failing, and returning ? and false on resolve, which is does not if the invoke-webrequest does not take place.

      I really hope someone can help me out here!

      Link to the script:

      Best regards

      • This topic was modified 1 month, 3 weeks ago by Jonatantwn72.
    • #246012
      Topics: 12
      Replies: 527
      Points: 1,247
      Helping Hand
      Rank: Community Hero

      $Rmatch in line 37 is not defined.
      Line 37 should say

      instead of

      But fundamentally, you should have separate try/catch blocks for resolve-dnsname and invoke-webrequest lines 11-47 as in:

      I would even recommend that you have a single [PSCustomObject] block and manipulate your output there as in:

    • #246015
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

      Try actually handling the error rather than trying to ignore it with Continue or SilentlyContinue. Even if it did “Continue”, you are then trying to get properties and perform replaces on a NULL value which should error. Try something more like this:

      Another note is rather than replacing, try parsing it as a URL to get portions:

    • #246285
      Topics: 8
      Replies: 9
      Points: 102
      Rank: Participant

      Thanks a lot Rob and Sam!

      To you Sam, i know the community should guide each other to get better and improve, and some prefer to give “hints” more than full usage examples. I do however greatly appreciate your example (and restructuring)!
      Makes it a lot easier for a rookie like me to understand the full picture.

      Just the feedback i needed 🙂

      Regarding your example Rob, to use parsing instead of replacing, i cant seem to make it work quite right.

      For an example, one domain i am scanning is, which redirects to
      Can parsing be used to identify this without involving a web-request?
      – sometimes the redirect presents itself in the hosting environment, but sometimes also through HTACCESS, which seems to make a difference as resolving DNS wont catch this.
      Sometimes i see domains redirected via. CNAME, but i many cases more “server locally” triggered.

      Can you elaborate please? 🙂

Viewing 3 reply threads
  • You must be logged in to reply to this topic.