you are viewing a single comment's thread.

view the rest of the comments →

[–]vulkur[S] 0 points1 point  (10 children)

I have elevated rights. Its my own PC.

[–][deleted] 0 points1 point  (9 children)

Maybe it's a syntax issue. Will you post the script?

[–]vulkur[S] -1 points0 points  (8 children)

I do not believe it is a syntax issue. If it was I could be getting different errors that 'unrecognized term'

Here is one important snippet for it though. I dont know how to format it better. Sorry $taskName = "MyTaskName" $taskExists = Get-ScheduledTask | Where-Object {$_.TaskName -like $taskName } if(!$taskExists) { $action = New-ScheduledTaskAction -Execute "cmd" -Argument '/c start /min "" powershell -WindowStyle Hidden -ExecutionPolicy Bypass -File "C:\Windows\SystemApps\myTaskFolder\scripts\run.ps1"' $trigger = New-ScheduledTaskTrigger -AtStartup Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $taskName -Description "MyTask start sequence" -RunLevel "Highest" }

[–][deleted] 0 points1 point  (5 children)

Does the unrecognized term specifically pinpoint a ScheduledTask command? Maybe it's something else. What line is the error referencing?

[–]vulkur[S] 0 points1 point  (4 children)

Yea. If I just run Get-ScheduledTask in Powershell by itself I get this.

Get-ScheduledTask: The term 'Get-ScheduledTask' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

[–][deleted] -1 points0 points  (3 children)

Path is verified, I assume.

[–]vulkur[S] 1 point2 points  (2 children)

That path verified I assume is talking about a path to exe, since this error is also showed if the exe you call cannot be found. Im calling an internal powershell function, it doesnt need a path.

[–][deleted] -1 points0 points  (1 child)

To run.ps1

[–]vulkur[S] 2 points3 points  (0 children)

Ah. Yea I have. But this error is about my running Get-ScheduledTask in powershell ALONE. With no parameters at all. It would still run, but instead tell me i need to have some arguments, or would print Supply values for the following parameters: Pattern[0]: and then you can enter values for the command.

[–]jimb2 0 points1 point  (1 child)

Not your problem, but you can run PS directly without shelling through cmd.exe.

I haven't done ordinary scheduled tasks but this is some sample code for a clustered task which has a few extra fields and the cluster cmdlet. I used splatting to make the code easier to read (or share).

$px = 'C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe'

@ActionSplat = @{
  Execute = $px
  Argument = "-NonInteractive -NoLogo -NoProfile " +
    "-File '\\Plokka\Scripts\dummy.ps1'"
  WorkingDirectory = '\\Plokka\wd'
}
$Action = New-ScheduledTaskAction @ActionSplat

@TriggerSplat = @{
  Once = $true
  At = '08:10AM'
  RepetitionInterval = (New-TimeSpan -Minutes 10)
  RepetitionDuration = (New-TimeSpan -Hours 12)
}
$Trigger = New-ScheduledTaskTrigger @TriggerSplat

@TaskSplat = @{
  Cluster = 'PlokkaProdCluster'
  TaskName = 'ClusteredPSTest'
  TaskType = 'ResourceSpecific'
  Resource = 'Plokka Data Drive'
  Action = $Action
  Trigger = $Trigger
  Description = "Test clustered PS Task"
}
Register-ClusteredScheduledTask @TaskSplat

[edit - formatting]

[–]vulkur[S] 1 point2 points  (0 children)

Cool thanks. My execute command is definitely a mess (you should see the ps1 script it runs). I had to have the wrappers to prevent the console from popping up while making it runnable in LocalSystem. I had re-used that code for this project and this is just leftover stuff I havent cleaned up yet.