0

I am working on PS script that will run a task once the computer has been logged in. Here is how I schedule the task:

$trigger = New-JobTrigger -AtLogOn
Register-ScheduledJob -Name TestSchedule -FilePath <filepath> -Trigger $trigger

The script scheduled to run does nothing but launch the command prompt, however nothing is being run once I log in to the computer. I have tried tinkering with it all I could but I get nothing.

alroc
  • 26,170
  • 5
  • 45
  • 88
RandellK02
  • 161
  • 1
  • 3
  • 17
  • Are you specifying the full (absolute) path to your script file? What happens if you manually execute the scheduled job, either through Task Scheduler or with `Get-ScheduledJob | Start-Job`? – alroc Jun 25 '13 at 02:16
  • @alroc I am using the full path. So far I tried to run it manually within task scheduler but doesnt work. I will try with `Get-ScheduledJob | Start-Job` – RandellK02 Jun 25 '13 at 17:27
  • You may need to run the job as admin. See here: https://stackoverflow.com/questions/6239647/using-powershell-credentials-without-being-prompted-for-a-password/6240319#6240319 and here: https://gist.github.com/wpsmith/4190b9a03bc1544a7df5 – felixmc Feb 16 '15 at 14:40

2 Answers2

0

Maybe not the best best solution but you could try putting the other script into this "job-script". Like This. Works Fine for me.

$jobname     = "xyz"
$JobTrigger  = New-JobTrigger -Weekly -At "03:00 AM" -DaysOfWeek Saturday
$MyOptions   = New-ScheduledJobOption -ContinueIfGoingOnBattery -HideInTaskScheduler
Register-ScheduledJob -name "$jobname" -scriptblock {$myscript} -trigger $JobTrigger –ScheduledJobOption $MyOptions
XXInvidiaXX
  • 263
  • 1
  • 7
  • 19
-1

I think your code lacks this command:

Set-ScheduledTask -TaskName $name -TaskPath Microsoft\Windows\PowerShell\ScheduledJobs -Principal (New-ScheduledTaskPrincipal -L Interactive -U $env:USERNAME)

$name is the name of your scheduledjob, this command makes the job interactive to the user, if your code still doesn't work, check out this script: https://gallery.technet.microsoft.com/scriptcenter/PC-Utilities-Downloader-355e5bfe

Tugrul Ates
  • 8,756
  • 1
  • 30
  • 51
k8C
  • 394
  • 3
  • 9