function ServiceRestart
{
Param
(
$ErrorLog,
$Services,
$MaxSize
)
$Time = Get-Date -Format 'yyyy:MM:dd HH:mm:ss'
$Result = (Get-Item $ErrorLog).length
if($Result -gt $MaxSize)
{
Clear-Content $ErrorLog
}
Try
{
Foreach($Service in $Services)
{
Restart-Service -DisplayName $Service -ErrorAction Stop
}
} Catch
{
"ERROR: $Service could not be restarted $Time" | Add-Content $ErrorLog
}
}
ServiceRestart -ErrorLog -Services -MaxSize
I need to pass in the following parameters from Task Scheduler
- Errorlog
- Services
- MaxSize
I currently have my Task Scheduler setup like this
Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add arguments(optional):
-Command "& \ServerName\C$\Users*****\Documents\Scripts\Scheduled-ServiceRestart.ps1
-ErrorLog 'ServerName\C$\Users*****\Documents\log\ScriptErrors.txt'
-Services 'foo1' , 'foo2'
-MaxSize '5MB'"
When I run the scheduled task nothing happens, what could be going wrong.