Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, November 21, 2018 3:32 PM
Trying to get ps1 running from Task Scheduler to check for specified services state & start if not running & mail result
And failing miserably in my scripting (inspired by this )
$srvservice=Get-Service
$svc1="service1"
$svc2="service2"
$counter
$counter=1
foreach($service in $srvservice)
{
if($srvservice[$counter].name -eq $svc1 -or $srvservice[$counter].name -eq $svc2)
{
if($srvservice[$counter].status -ne 'Running')
{ start-service -name $srvservice[$counter].name
Start-Sleep -seconds 30
} else
{
if($srvservice[$counter].status -eq 'Running')
{
send-mailmessage -from [email protected] -to [email protected] -subject 'The Background Processing Service service was started' -body 'It was started by scheduled task check' -smtpserver 10.0.0.127
} else
{
send-mailmessage -from [email protected] -to [email protected] -subject 'The Background Processing Service service failed to start' -body 'It was also NOT started by scheduled task check, please investigate' -smtpserver 10.0.0.127
}
}
}
$counter++
}
Basically, first service starts, but the second does not
start-service : Service 'Some Service (Service2)' cannot be started due to the following error: Cannot open Service2 service on computer '.'.
It does work fine if started from services.msc, so the error message is rubish
If I change the script to have only single service to monitor, then start works fine, but script never gets to the second if
if($srvservice[$counter].status -eq 'Running')
and mail does not get sent
All replies (16)
Wednesday, November 21, 2018 5:12 PM ✅Answered
$myServiceArray=@()
$myServiceArray="wuauserv","WinRM"
Foreach($Service in $myServiceArray){
Try {
$Computername=$env:COMPUTERNAME
$Result=Get-Service -ComputerName $Computername -Name "$Service" -ErrorAction Stop | select -ExpandProperty Status
if ($Result -ne "running")
{
Start-Service -name $Service
$Final=Get-Service -ComputerName $Computername -Name "$Service" -ErrorAction Stop | select -ExpandProperty Status
#
# Configure Mail Variables
#
$SMTPServer = "YourMailServer"
$SMTPFrom = "[email protected]"
$SMTPTo = "[email protected]"
$SMTPSubject = "$Computername Error $Service Not Running"
$SMTPBody = "$Computername Service $Service Not Running - Attempted Start - Currnet Status = $Final"
#
#Send Mail and Attach Custom Named Log File
#
Send-MailMessage -SmtpServer $SMTPServer -From $SMTPFrom -To $SMTPTo -Subject $SMTPSubject -Body $SMTPBody
}
}
Catch [Exception]
{
#
# Configure Mail Variables
#
$SMTPServer = "YourMailServer"
$SMTPFrom = "[email protected]"
$SMTPTo = "[email protected]"
$SMTPSubject = "$Computername Error Connection | Service Name"
$SMTPBody = "$Computername Service $Service Service not Found or Server Not Reachable - $_"
#
#Send Mail and Attach Custom Named Log File
#
Send-MailMessage -SmtpServer $SMTPServer -From $SMTPFrom -To $SMTPTo -Subject $SMTPSubject -Body $SMTPBody
}
}
Wednesday, November 21, 2018 3:55 PM
Not rubbish. Code is not even close.
$services = 'service1','service2'
foreach($service in $services){
Try{
$s = Get-Service $service -ErrorAction Stop
if($s.Status -ne 'Running'){
start-service $service -ErrorAction Stop
send-mailmessage -from [email protected] -to [email protected] -subject 'The Background Processing Service service was started' -body 'It was started by scheduled task check' -smtpserver 10.0.0.127
}
}
Catch{
Throw $_
}
}
This will give a detailed error message for all reasons for an error.
\(ツ)_/
Wednesday, November 21, 2018 4:22 PM
Exactly what I said, no matter how one does arrive at it.
Same rubbish error message:
PS C:\scripts> C:\scripts\service_multi_restart_v3.ps1
start-service : Service 'Reports Service (ReportsMain)' cannot be started due to the following error: Cannot open ReportsMain
service on computer '.'.
At C:\scripts\service_multi_restart_v3.ps1:6 char:13
+ start-service $service -ErrorAction Stop
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Start-Service], ServiceCommandException
+ FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.StartServiceCommand
It ONLY starts service1, service2 always fails, yet that service EXISTS, and it can perfectly be started by hand in services console, service has NO dependencies
I also want to send mail ONLY when the service is actually running & not straight after it was started (hence the second check)
And as result no message is sent, so it is far from a solution!
Wednesday, November 21, 2018 4:27 PM
That is not the code's problem. It is the service that is causing that. The service is complaining that the "main" of the service cannot be opened. Contact the service vendor for help. It may be that you have issues with dependencies.
We cannot fix your system for you.
\(ツ)_/
Wednesday, November 21, 2018 4:32 PM
..................
Wednesday, November 21, 2018 4:37 PM
Then you did not use my code.
\(ツ)_/
Wednesday, November 21, 2018 5:02 PM
Really, all this negative energy? I am sure you are miles better at PS coding that I ever would be!
OK, for some reason for PS to be able to start the service I needed to change permissions on the service (add myself), or run PS as Administrator
Strangely with YOUR code, I receive 2 mails (with same text), but to me it is best guess that the service actually started (potentially it could start & fail, but mail would still go through before)
Which one can simulate with addition of extra line:
Stop-Service $service -ErrorAction Stop
and the mail still goes fine
Seb
Wednesday, November 21, 2018 5:22 PM
Nice ONE ComputerScott!
Simple and effective
Wednesday, November 21, 2018 5:33 PM
$myServiceArray=@() $myServiceArray="wuauserv","WinRM" Foreach($Service in $myServiceArray){ Try { $Computername=$env:COMPUTERNAME $Result=Get-Service -ComputerName $Computername -Name "$Service" -ErrorAction Stop | select -ExpandProperty Status if ($Result -ne "running") { Start-Service -name $Service $Final=Get-Service -ComputerName $Computername -Name "$Service" -ErrorAction Stop | select -ExpandProperty Status # # Configure Mail Variables # $SMTPServer = "YourMailServer" $SMTPFrom = "[email protected]" $SMTPTo = "[email protected]" $SMTPSubject = "$Computername Error $Service Not Running" $SMTPBody = "$Computername Service $Service Not Running - Attempted Start - Currnet Status = $Final" # #Send Mail and Attach Custom Named Log File # Send-MailMessage -SmtpServer $SMTPServer -From $SMTPFrom -To $SMTPTo -Subject $SMTPSubject -Body $SMTPBody } } Catch [Exception] { # # Configure Mail Variables # $SMTPServer = "YourMailServer" $SMTPFrom = "[email protected]" $SMTPTo = "[email protected]" $SMTPSubject = "$Computername Error Connection | Service Name" $SMTPBody = "$Computername Service $Service Service not Found or Server Not Reachable - $_" # #Send Mail and Attach Custom Named Log File # Send-MailMessage -SmtpServer $SMTPServer -From $SMTPFrom -To $SMTPTo -Subject $SMTPSubject -Body $SMTPBody } }
Will not work due to a few errors in the code. You need to test a bit more.
\(ツ)_/
Wednesday, November 21, 2018 5:39 PM
Really, all this negative energy? I am sure you are miles better at PS coding that I ever would be!
OK, for some reason for PS to be able to start the service I needed to change permissions on the service (add myself), or run PS as Administrator
Strangely with YOUR code, I receive 2 mails (with same text), but to me it is best guess that the service actually started (potentially it could start & fail, but mail would still go through before)
Which one can simulate with addition of extra line:
Stop-Service $service -ErrorAction Stop
and the mail still goes fineSeb
No. This is really a lack of knowledge of how a service works and how the CmdLets work. A service starts and runs then the CmdLet returns. If the CmdLet fails it will throw an exception. If the service fails it will throw an exception. If the service is no running it starts it. If it is running it does not try to start it.
There is no need for a huge amount of code to do this. We do it all of the time. A will admit I didn't account for your email messages so you could get two but that is an easy fix.
What hostility? You complained about the message. They are very complete and useful if you take the time to both learn how to read them and a little more time to learn PowerShell.
\(ツ)_/
Wednesday, November 21, 2018 7:11 PM
If you say...
To me error message like this makes no sense
Cannot open ReportsMain
service on computer '.'.
That is like users helpdesk request, "My Computer does not work", but how, why, what?
I have tested ComputerScott's script & see nothing wrong with it (worked doing what I need)
Wednesday, November 21, 2018 7:47 PM
The code is just wrong.
Start-Service -name $Service
This will always start a service on the local computer and never on the remote computer.
It is similar to my code but has too many bits that are wrong or un necessary.
Look closely at all code offered and be sure it is correct and does what it is supposed to do.
\(ツ)_/
Wednesday, November 21, 2018 10:05 PM
Thanks, but relax, no need to be so negative, it is not healthy!
Wednesday, November 21, 2018 10:53 PM
Thanks, but relax, no need to be so negative, it is not healthy!
Where am I being negative. The truth is just the truth. Why do you people new to engineering and deep technology always get bent when someone tells you something is wrong? How can you become a professional if you can't tolerate criticism?
I post to help people see what they are doing and to point them at better and correct methods. I am pretty sure that after 40_ years I have learned abit more than you. Sit back and be thankful that someone is taking an interest.
\(ツ)_/
Thursday, November 22, 2018 10:29 AM
I am sorry, was not going to reply, but your attitude is really upsetting.
If anything, it seems to me that it is you that thinks that you are the one & only
I have already admitted this: I am sure you are miles better at PS coding that I ever would be!
Maybe it is better not to bother to reply. You will get stressed less
Thursday, November 22, 2018 12:08 PM
I am sorry, was not going to reply, but your attitude is really upsetting.
If anything, it seems to me that it is you that thinks that you are the one & only
I have already admitted this: I am sure you are miles better at PS coding that I ever would be!
Maybe it is better not to bother to reply. You will get stressed less
Stressed? You ae the one complaining. Your problem now.
\(ツ)_/