powershell and Scheduled Task

Good morning everyone, I have a small problem regarding a powershell script and scheduled operations.

The script below should stop active processes on a server and restart them and send an email.

If I do it step by step there are no problems everything works correctly if instead I try to insert it a scheduled operation I only get the notification email but the processes don’t stop e don’t restart

Stop-process -Name tcpnetmgr
Stop-process -Name tcpbpemgr
Stop-process -Name tcpbtmgr
Stop-process -Name tcpcommgr
Start-Process -FilePath "C:\fe\tcpnetmgr.exe" -WindowStyle Minimized
Start-Process -FilePath "C:\fe\tcpbpemgr.exe" -WindowStyle Minimized
Start-Process -FilePath "C:\fe\tcpbtmgr.exe" -WindowStyle Minimized
Start-Process -FilePath "C:\fe\tcpcommgr.exe" -WindowStyle Minimized

$Username = 'info@localdomain.it'
$Password = 'XXXXXXXXXXXX'
$Secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Mycreds = New-Object System.Management.Automation.PSCredential ($Username, $secpasswd)
$From = "IFE1@localdomain.it"
$Cc4 = "prova@localdomain.it"
$Subject = "Riavvio Servizi IFE1"
$Body = "Email inviata per confermare il corretto riavvio dei servizi su IFE1"
$SMTPServer = "smtp.localdomain.it"
Send-MailMessage -From $From -to $Cc4 -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port 25 -Credential $Mycreds –DeliveryNotificationOption OnSuccess

Would anyone know how to make suggestions?

I can’t understand why the processes are not stopped and restarted and the email is sent being part of the same code.

Thanks everyone for your attention

If your processes are properly registered, then you shouldn’t have to specify Filepath, but just the name of the process.

 

Also, are you running it as administrator? If you are, then I’d recommend setting your $Errorpreference to STOP, and wrap it all in Try/Catch blocks to see where it fails, and to see the error.

 

Maybe I explained myself wrong or google translate has translated incorrectly.
If I manually run my scritp everything works perfectly.
The applications are stopped and restarted without problem and an email is sent to report the restart.

If I try to schedule the .ps1 file then nothing works.

I am only sent the restarted email, the applications are never stopped and restarted.

I did a test trying to run maximized notepad with consequent sending of the email.
The notepad does not start but the email is sent.

The operation is scheduled with an administrative user.

Can you share the details of your Scheduled Task?

You may want to consider using Start-Transcript and Stop-Transcript in your script (or even in the task setup) to capture everything that’s going on during the execution of the scheduled task. This method allows you actually see what is happening and when.

Add the try/catch as suggested but use the St*-Transcript cmdlets to output to a file.

[quote quote=243134]Maybe I explained myself wrong or google translate has translated incorrectly.

If I manually run my scritp everything works perfectly.

The applications are stopped and restarted without problem and an email is sent to report the restart.

If I try to schedule the .ps1 file then nothing works.

I am only sent the restarted email, the applications are never stopped and restarted.

I did a test trying to run maximized notepad with consequent sending of the email.

The notepad does not start but the email is sent.

The operation is scheduled with an administrative user.
[/quote]

I don’t see anything in your script referring to Notepad?
Is the scheduled task set to run as Administrator? If not, then it will most likely not be able to stop the processes.

If you run it from a normal console, does it work?
If it only works from a console with admin rights, then the scheduled task also needs to run with admin rights.

Also, I always suggest to people to break something that doesn’t work down into each element.
What happens when you follow my suggestion and put each element in a Try/Catch block and log the output?

Thanks for the tips.
The script I created, if run from the powershell console, works correctly.
If I run the .ps1 file it works correctly.

Stops the services that must be stopped, restarts them and sends the email of the restart of the processes.

In the scheduler, the script was run with administrator privileges.

Temporarily I solved the problem by changing the scheduling configurations; instead of running the script, whether the user was logged in or not, I put it into execution only if the user is logged in.

Now works.
I just have to understand how to make it run with or without the logged in user.

Meanwhile the script has been revised a bit.

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

$LogFile = ("C:\Script\Log\IFE1-" + (Get-Date -Format "dd-MMM-yyyy") + ".txt")

("Ora inizio ") + (get-date -format "HH:mm:ss") >> $LogFile
'1 ------- Verifica che i processi siano in esecuzione' >> $LogFile
get-process tcpnetmgr >> $LogFile
get-process tcpbpemgr >> $LogFile
get-process tcpbtmgr >> $LogFile
get-process tcpcommgr >> $LogFile
'2 ------- Stop dei processi tcpnetmgr,tcpbpemgr,tcpbtmgr,tcpcommgr' >> $LogFile
Stop-process -Name tcpnetmgr
Stop-process -Name tcpbpemgr
Stop-process -Name tcpbtmgr
Stop-process -Name tcpcommgr
'3 ------- Verifica che i processi tcpnetmgr,tcpbpemgr,tcpbtmgr,tcpcommgr siano stati stoppati correttamente' >> $LogFile
Try
{
get-process tcpnetmgr -ErrorAction Stop
get-process tcpbpemgr -ErrorAction Stop
get-process tcpbtmgr -ErrorAction Stop
get-process tcpcommgr -ErrorAction Stop
}
Catch
{
"4 ------- PROCESSI NON ATTIVI" >> $LogFile
}

'5 ------- Avvio dei processi tcpnetmgr,tcpbpemgr,tcpbtmgr,tcpcommgr' >> $LogFile
("Ora riavvio applicazioni ") + (get-date -format "HH:mm:ss") >> $LogFile
Start-Process -FilePath "C:\fe\tcpnetmgr.exe" -WindowStyle Minimized
Start-Process -FilePath "C:\fe\tcpbpemgr.exe" -WindowStyle Minimized
Start-Process -FilePath "C:\fe\tcpbtmgr.exe" -WindowStyle Minimized
Start-Process -FilePath "C:\fe\tcpcommgr.exe" -WindowStyle Minimized
'6 ------- Verifica che i processi tcpnetmgr,tcpbpemgr,tcpbtmgr,tcpcommgr siano stati riavviati correttamente' >> $LogFile
("Ora verifica") + (get-date -format "HH:mm:ss") >> $LogFile
get-process tcpnetmgr >> $LogFile
get-process tcpbpemgr >> $LogFile
get-process tcpbtmgr >> $LogFile
get-process tcpcommgr >> $LogFile
'7 ------- Invio EMAIL' >> $LogFile
$Username = 'XXXXXXXXXX'
$Password = '!XXXXXXXXXX'
$Secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Mycreds = New-Object System.Management.Automation.PSCredential ($Username, $secpasswd)
$From = "IFE1@localdomain.it"
$To = "XXX@localdomain.it"
$Cc = "XXX@localdomain.it"
$Cc2 = "XXX@localdomain.it"
$Cc3 = "XXX@localdomain.it"
$Cc4 = "XXX@localdomain.it"
$Cc5 = "XXX@localdomain.it"
$Subject = "Riavvio Servizi IFE1"
$Body = Get-Content -Path $LogFile | Out-String
$SMTPServer = "smtp.qsave.it"
Send-MailMessage -From $From -to $To,$Cc,$Cc2,$Cc3,$Cc,$Cc4,$Cc5 -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port 25 -Credential $Mycreds -Attachments $Attachment –DeliveryNotificationOption OnSuccess
#Send-MailMessage -From $From -to $Cc4,$Cc5 -Subject $Subject -Body $Body -SmtpServer $SMTPServer -port 25 -Credential $Mycreds -Attachments $LogFile –DeliveryNotificationOption OnSuccess

 

Are you opening Task Scheduler as Administrator when setting up the task? Does that user account have the Log on as a batch job user right?

Log on as a batch job (Local Security Policy):
https://www.brooksnet.com/faq/granting-logon-as-batch-privilege

Log on as a batch job (Group Policy):
GPMC > Computer Configuration\Windows Settings\Security Settings\Local Policies\User Rights Assignment

I opt for Group Managed Service accounts for scheduled tasks wherever possible as there is literally no management overhead, so no dealing with compromised/expired passwords, etc.

This is obviously going beyond the scope of PowerShell scripting but take a look into these things for your script to start working unattended.