Tricky Powershell Script - Need to invoke web request check a status of service first

Okay - Basic request - our server has an application that syncs data from my server to an external server (software provider that our end users use). The service basically runs an API/query - I need to write a power shell script to check the status of the data sync and if status =’0’ then re-start the service otherwise don’t do anything. First step - run this script: Invoke -WebRequest -Uri http://localhost:59999/api/SyncProcess -UseBasicParsing - script will return a “Status” (0 or 1 or 5) - based upon the status I need to quit (stop if status not ‘0’) otherwise stop and re-start a service (I got this part lots of scripts to stop and re-start available) just don’t know how to incorporate the invoke web request - read status part of the script.

What have you tried?

What does the response that returns the status actually look like? We cannot provide detailed help without that information.

Essentially, you need to capture the response to a variable, find the status object and get its value. If the response is JSON, consider using Invoke-RestMethod instead.

Possible examples, assumes it’s not a nested object:

# XML response example
$response = Invoke-WebRequest -Uri http://localhost:59999/api/SyncProcess -UseBasicParsing

$status = ([xml]$response.Content).Status

if ($status -ne 0} {
    Exit
}

# rest of script
# JSON response example
$response = Invoke-RestMethod -Uri http://localhost:59999/api/SyncProcess -UseBasicParsing

if ($response.Status -ne 0} {
    Exit
}
1 Like

The status is nested - I haven’t tried anything yet. Your thoughts? I have to reference the content section of the response?

My thoughts: the code won’t write itself :slight_smile:

That’s a JSON response, and it’s not nested. Try the example I posted.

2 Likes

Okay - here we go:

$response = Invoke-WebRequest -Uri http://localhost:59999/api/SyncProcess -UseBasicParsing

if ($response.Status - ne 1,2) {

Exit

}

“Stop-Service-SmartSyncPolllingService - Force

Start-Service-SmartSyncPollingService”

Exit

Basically if status is not 1 or 2 - then stop and start the service

Please use the </> button to format your post when posting code.

What you’ve posted won’t work because you’re still using Invoke-WebRequest, but you’re not converting the response to JSON.

Because you know the response is JSON, it would be simpler to use Invoke-RestMethod which will convert the response for you. Please re-read my example.

Your conditional statement is written incorrectly. If you want to check if the status code is in an array of values, use -notin.

Your syntax for Stop-Service / Start-Service is also incorrect and will give you errors.

2 Likes

Thank you for hanging in there with me. I really appreciate it.

This appears to be working for me - feel free to critique.

$response = Invoke-RestMethod -Uri http://localhost:59999/api/SyncProcess -UseBasicParsing

if ($response.Status -notin 1,2) {

Exit

}

“$services = Get-Service - SmartSyncPollingService

Restart-Service $Services -Force”

Exit

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.