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"
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
I had the same problem.
executer.ps1
file which was trying to execute backup.ps1
file.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.-File
to the line and explicitly wrote the path between the quotes but didn't work for me.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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.