Pass Powershell parameters within Task Scheduler
Asked Answered
M

5

15
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.

Mcdermott answered 16/6, 2017 at 16:42 Comment(0)
R
15

I recommend scheduling the task to use the -File parameter rather than -Command. Example:

Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments (optional): -NoProfile -ExecutionPolicy Bypass -File "Scheduled-ServiceRestart.ps1" -ErrorLog "ScriptErrors.txt" -Services "foo1","foo2" -MaxSize 5MB

Start in (optional): C:\Users\<username>\Documents\Scripts

You can specify the starting directory for the script in the "Start in" property for the task and avoid the lengthy path names to the script and log files. (Note that I am assuming you are running a copy of the script on the local computer, not over the network, which adds potential complications and possibilities for failure.)

Recapitulate answered 16/6, 2017 at 17:32 Comment(3)
Using -NoProfile -ExecutionPolicy Unrestricted -File 'Scheduled-ServiceRestart.ps1' -ErrorLog '<servername>\C$\Users\<username>\Documents\log\ScriptErrors.txt' -Services 'Service1','Service2' -MaxSize '500MB'" I get error (0xFFFD0000). I am running this on a remote server but the script only needs to execute locally. @RecapitulateMcdermott
Copy the script to the local server.Recapitulate
I was unable to use the example shown here due to the single quotes surrounding parameter values like 'Scheduled-ServiceRestart.ps1'. Instead, I replaced all single quotes with double quotes and that fixed all of my issues. If found when dealing with Windows command line, you should use double quotes as single quotes are only used in rare situations.Fleawort
E
4

The function needs to be imported first. I would recommend saving the function as a module and placing it in the modules folder in either system32 or program files. This way, when powershell is launched, it will automatically import your function.

After you do that, the task scheduler is very simple.

Program/Script

Powershell

Add arguments(optional):

-Command &{ServiceRestart -ErrorLog 'ServerName\C$\Users*****\Documents\log\ScriptErrors.txt' -Services 'foo1','foo2' -MaxSize '5MB'}
Exportation answered 16/6, 2017 at 20:17 Comment(0)
E
2

Apparently, when using Scheduled Tasks, the choice of quotes makes a huge difference. Try this for your arguments:

-Command "& '\\ServerName\C$\Users\...\Documents\Scripts\Scheduled-ServiceRestart.ps1' -ErrorLog '\\ServerName\C$\Users\...\Documents\log\ScriptErrors.txt' -Services 'foo1','foo2' -MaxSize '5MB'"
Exstipulate answered 17/10, 2018 at 13:43 Comment(0)
C
1

As far as a literal answer to the question: Likely you need to add single quotes to surround your script location, otherwise it will try to interpret special characters/backslashes as escapes.

Ex:

-Command "& '\ServerName\C$\Users*****\Documents\Scripts\Scheduled-ServiceRestart.ps1' ...

You could also add some basic logging in your powershell script to ensure that it is actually starting.

I use the "-Command &" style in production Powershell scheduled tasks & they work fine provided the strings are formatted properly.

Com answered 13/11, 2017 at 21:44 Comment(0)
T
0

In addition to Bill's suggestion, another thing to look out for is the "Configure for:" setting in the General tab of Task Scheduler. Depending on your setup it may be defaulting to an earlier version of Windows than the one you're running, and in the case of Server 2008 it looks like it doesn't support adding parameters like that to the .ps1 file you're calling. Took me a while to work out after trying the other suggestions here.

For example :

-file "c:\scripts\myscript.ps1"

Works fine in task scheduler

.\myscript.ps1 -myparameter foo

Works fine from the shell, however :

-file "c:\scripts\myscript.ps1" -myparameter foo

In task scheduler fails to run with a 0xfffd0000 result code.

Spotted that "Configure for:" had defaulted to 2008, so changed that to 2012 R2 and that fixed the issue and it's now running fine.

Torosian answered 9/12, 2021 at 16:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.