Consistent results in -LIKE & -MATCH

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

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

[quote quote=237907]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
[/quote] 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

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?

[quote quote=238187]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?

[/quote]
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
}

}