$ip_addr = "localhost" $port = "8080" $seconds = 10 $url = "http://" + $ip_addr + ":" + $port + "/elx/do/eno/co/query/system-status"; function pid_port_processFunction { # Get a list of all active TCP connections $connections = Get-NetTCPConnection # Filter the list to only include connections in the "Listen" state $listeningConnections = $connections | Where-Object { $_.State -eq "Listen" } # Get the PIDs of the processes associated with the connections $pids = $listeningConnections | Select-Object -ExpandProperty OwningProcess # Get the process objects for the PIDs $processes = Get-Process -Id $pids # Filter the list of processes to only include Java processes $javaProcesses = $processes | Where-Object { $_.ProcessName -eq "java" } # Print the port numbers, PIDs, and process names of the Java listening processes foreach ($process in $javaProcesses) { $connection = $listeningConnections | Where-Object { $_.OwningProcess -eq $process.Id } Write-Host "Port: $($connection.LocalPort)" Write-Host "PID: $($process.Id)" Write-Host "Process name: $($process.ProcessName)" Write-Host } } #This print to screen if jobEnginesCount is less than 2 #Use this to send an email or call an action function testFunction { Write-Host "JobEnginesCount < 2" pid_port_processFunction } #This print to screen the values of data #You can omit this as print to screen also takes up resources function healthFunction { cls Write-Host "userActivityCount= $($json.usersActivityCount)" Write-Host "jobCurrentQueueSize= $($json.jobCurrentQueueSize)" Write-Host "jobEnginesCount= $($json.jobEnginesCount)`n" pid_port_processFunction $url ="" } #This loops the health if it is jobEngineCount is 2 while ($true){ try { $json = Invoke-WebRequest -Uri $url | ConvertFrom-Json switch ($json.jobEnginesCount) { 0 { testFunction } 1 { testFunction } default { healthFunction } } } catch [System.Net.WebException] { $ex = $_.Exception CLS Write-Host "An error occurred while sending the request: $($ex.Message)" # you can log the exception message or do any other error handling } #This sets the number of seconds it loops Start-Sleep -Seconds $seconds }