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"
      $WebResponse.Content

      if($WebResponse.Content -like '*Height="100%"*'){

      #We have it!
      Write-Host "$_ Passed!"
      }
      else {

      #We don't have it
      Write-Host "Fail"
      }

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

      Regards,
      Yarrokon

      • This topic was modified 1 week, 6 days ago by Yarrokon38.
    • #237907
      js
      Participant
      Topics: 30
      Replies: 828
      Points: 2,556
      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.

      'Height="100%"' -like '*Height="100%"*'
      
      True
      
      
      'Height="100%"' -match 'Height="100%"'
      
      True
      • This reply was modified 1 week, 6 days 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;

      hostname "SHORT_HOST_NAME"
      return_code 0
      message "Successfully updated and fixed SHORT_HOST_NAME."
      success true

      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.

      Get-Content "Hosts2.txt" | ForEach-Object {
      
          # Setting Variables + Cleaning. I've had to add this as the $message data does not get cleaned between hosts Failed $messages are empty.
          $invokeData = ""
          $invokeData_json = ""
          $success = ""
          $message = ""
      
          # Write-Host "Checking for $_"
          $invokeData = (invoke-webrequest "web_address/$_" | Select-Object -Property Content).content
      
          # Converting Json
          $invokeData_json = $invokeData | Out-String | ConvertFrom-Json
          $success = $invokeData_json.success
          $message = $invokeData_json.message
      
          if($success -eq "True") {
              Write-Host "$_ - Passed! - $message"
              Write-Host
      
          }
          else {
              Write-Host "$_ - FAILED! - $message"
              Write-Host
          }
      }
Viewing 4 reply threads
  • You must be logged in to reply to this topic.