R
R
radioactivetoy2022-04-07 11:18:46
PowerShell
radioactivetoy, 2022-04-07 11:18:46

How to poll a web address in a script?

Hello, I needed to make a script to check the operation of a program / service and restart it.
I will make a reservation that I am new to PS, but by googling for various examples I wrote what is required, and in general it works.
Having omitted the unnecessary context, the problem is that I need to check the availability of the program's web interface, I implemented this through the function

if  ((Test-NetConnection 127.0.0.1 -Port 8088).TcpTestSucceeded)

и сделав задание по запуску скрипта каждые 10 минут. Немного топорно, но устаривало.

Собственно сейсач хотелсь бы это переделать, что бы опрос шел непрерывно, по типу пинга.
Подскажите как можно это реализовать, погуглив не особо удалось найти решение которое бы устраивало.
Полный скрипт если что в спойлере
spoiler
$datetime = Get-Date
$From = "..."
$To = "..."
$subject = "...."
$body = "..."
$SMTPServer = "..."
$SMTPPort = "..."
$Username = "..."
$Password = "..."

if ((Test-NetConnection 127.0.0.1 -Port 8088).TcpTestSucceeded)
{
"$datetime | $env:COMPUTERNAME | VCA Core works" | out-file C:\VCACore\Raport.txt -append
write-host "Works" -ForegroundColor Green
}

else {
"$datetime | $env:COMPUTERNAME | VCA Core fucked up!!!" | out-file C:\VCACore\Raport.txt -append
Restart-Service "VCA Core"
write-host "Restarted" -ForegroundColor Yellow
timeout /t 10

if ((Test-NetConnection 127.0.0.1 -Port 8088).TcpTestSucceeded)
{
"$datetime | $env:COMPUTERNAME | Service has restarted" | out-file C:\VCACore\Raport.txt -append
write-host "Restarted" -ForegroundColor Yellow
}

else
{
"$datetime | $env:COMPUTERNAME | VCA CORE FUCKED UP & Mail has been sent " | out-file C:\VCACore\Raport.txt -append

$message = New-Object System.Net.Mail.MailMessage $From, $To
$message.Subject = $subject
$message.IsBodyHTML = $true
$message.Body = $body

$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort)
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
$smtp.Send($message)

}
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Morrowind, 2022-04-07
@radioactivetoy

Hey!
If I understand correctly that you just need to make an endless Ping on the current code, then just wrap its functionality in a while loop .
It will be possible to cancel it through CTRL + C in the console.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question