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
[/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.
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;
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
}