Consistent results in -LIKE & -MATCH

Welcome Forums General PowerShell Q&A Consistent results in -LIKE & -MATCH

Viewing 4 reply threads
  • Author
    Posts
    • #237886
      Participant
      Topics: 1
      Replies: 2
      Points: 18
      Rank: Member

      I’m trying to search for double quoted text in a webrequest and can’t seem to get a consistent pass / fail. I’ve tried -like with * and I’ve tried -match without *, is the issue how I’m getting around the double quotes that I’m searching for?

      I’ve been using the below for testing, but the actual string that I need to find in my project is

      “success”: true,

      $WebResponse = invoke-webrequest "http://www.ANYDOMAIN.com"<br />$WebResponse.Content</p><p>if($WebResponse.Content -like '*Height="100%"*'){</p><p>#We have it!<br />Write-Host "$_ Passed!"<br />}<br />else {</p><p>#We don't have it<br />Write-Host "Fail"<br />}

      I appreciate any help or even pointing me in a direction to read up on something.

      Regards,
      Yarrokon

      • This topic was modified 2 months, 3 weeks ago by Yarrokon38.
    • #237907
      js
      Participant
      Topics: 32
      Replies: 842
      Points: 2,686
      Helping Hand
      Rank: Community Hero

      Works for me. I don’t see what “$_” would be, unless you’re inside a loop you’re not showing.

      • This reply was modified 2 months, 3 weeks ago by js.
    • #237928
      Participant
      Topics: 1
      Replies: 2
      Points: 18
      Rank: Member

      Works for me.

      PowerShell
      8 lines

      <textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59766px; left: 44px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>

      1
      2
      3
      4
      5
      6
      7
      8
      ‘Height=”100%”‘ -like ‘*Height=”100%”*’
      True
      ‘Height=”100%”‘ -match ‘Height=”100%”‘
      True
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      ty for checking, I still get failures, maybe the network / system I’m on. no clue why it would fail like I’m seeing. TY Again for check, appreciate it.

      Regards,
      Yarrokon

    • #238187
      Participant
      Topics: 6
      Replies: 17
      Points: 132
      Helping Hand
      Rank: Participant

      Given that you’re looking for "success": true, I’m guessing you’re working with an API that’s returning a JSON object? I recommend using Invoke-RestMethod if you can.

      Maybe you could show us a sample of the $WebResponse.Content you’re getting, and what you’re expecting?

    • #238274
      Participant
      Topics: 1
      Replies: 2
      Points: 18
      Rank: Member

      Given that you’re looking for "success": true, I’m guessing you’re working with an API that’s returning a JSON object? I recommend using Invoke-RestMethod if you can.

      Maybe you could show us a sample of the $WebResponse.Content you’re getting, and what you’re expecting?

      You are very correct. I’m actually attempting to convert the JSON. I’m looking for;

      I’ve updated the script a bit from above. I can now get success and fail. Success will print the Message out, fail does not for some reason. I am now getting correct responses when testing.

      Thank you for looking.

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