1

I'm working on a test script to get the idea of working with the Task Scheduler in PowerShell.

I don't want to have any credentials within the script for security matters.

My idea was to create a task in PowerShell which runs a script but for some reason it won't execute properly and I don't get why.

My task is created as following:

$taskName = "WeeklyMaintance"
$User = "NT AUTHORITY\SYSTEM"
$Trigger = New-ScheduledTaskTrigger –Daily -At "08:14"
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "D:\Some SW\_Scripts\testing.ps1"
Register-ScheduledTask -TaskName $taskName -Trigger $Trigger -User $User -Action $Action -RunLevel Highest

and the script looks like this

$Logpath = "D:\Some SW\_Scripts"
$logname = "Log.txt"
function Write-Log {
    Param ([string]$logstring)
    $timestamp = Get-Date -Format "dd.MM.yyyy HH:mm:ss"
    $log = "[$timestamp]: $logstring"
    Add-Content -Value $log -Path "$Logpath\$logname"
}
Write-Log "My Test for research worked"

The Logfile is already created and this works perfectly fine when executed normaly but for some reasen as I create the task and run it with the task it won't start.

Did I something wrong with the creation of the task or using the system here?

Edit:

I found out it was only the path which must have been the "bad guy".

As I changed the path from "D:\Some SW_Scripts" to "D:\Test"

2 Answers 2

3

I have had issues with this in the past and I've needed to add the '-File' parameter to the action.

$taskName = "WeeklyMaintance"
$User= "NT AUTHORITY\SYSTEM"
$Trigger= New-ScheduledTaskTrigger –Daily -At "08:14"
$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-File `"D:\Some SW\_Scripts\testing.ps1`""
Register-ScheduledTask -TaskName $taskName -Trigger $Trigger -User $User -Action $Action -RunLevel Highest
Sign up to request clarification or add additional context in comments.

3 Comments

@Kevin The code also adds double quotes around the path, which should take care of what you described as the cause of the issue in your own answer.
@AnsgarWiechers are there any diffrences in quotas and double quotas ? Or should I maybe have added those quotas in the "argument" itself ?
@Kevin You need double quotes around the path string (single quotes won't work). For nesting double quotes in a double-quoted string (like the answer does) you must escape the nested double quotes with backticks (like the answer does). Alternatively you could use a single-quoted string, in which case the nested double quotes must not be escaped (-Argument '-File "D:\Some SW\_Scripts\testing.ps1"')
0

I had the same problem.

  • I had an executer.ps1 file which was trying to execute backup.ps1 file.
  • When I tried to run the backup.ps1 file directly, it was perfectly fine; however, when I tried to execute it within the executer.ps1, the New-ScheduledTaskAction was unable to execute it.
  • As @Ash mentioned, I added -File to the line and explicitly wrote the path between the quotes but didn't work for me.
  • Finally, I modified the line as: New-ScheduledTaskAction -Execute $powerShellExe -Argument "-executionpolicy bypass -noprofile -file $scriptDirectory" and it worked for me.
[string]$scriptDirectory = "C:\Users\<username>\Desktop\PS\backup.ps1"
[string]$powerShellExe = "PowerShell.exe"
[string]$taskName = "BackupTask"
[string]$description = "Test backup task"
[string]$userId = "NT AUTHORITY\SYSTEM"

$trigger = New-ScheduledTaskTrigger -Daily -DaysInterval 3 -At 12:58:05pm
$principal = New-ScheduledTaskPrincipal -UserID $userId -LogonType S4U
$settings = New-ScheduledTaskSettingsSet -MultipleInstances Parallel -DontStopIfGoingOnBatteries
$action = New-ScheduledTaskAction -Execute $powerShellExe -Argument "-executionpolicy bypass -noprofile -file $scriptDirectory"  # Specify what program to run and with its parameters

Register-ScheduledTask -TaskName $taskName -Trigger $trigger -Action $action -Description $description -Settings $settings -Principal $principal

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.