0

I'm trying to schedule a task that runs a PS script with parameters. But I believe I'm missing an escape character somewhere or my ' and " are in the wrong place. I can't seem to get it to work properly without it splitting the action argument. Below is my code:

    $employeeID = "1234"
    $employee = "email@email.com"
    $restoretime = (Get-date).AddHours(12)
    $restoreuser = "$($employee) Offboarding"
    $trigger = New-ScheduledTaskTrigger -once -At $restoretime 
    $taskAction = New-ScheduledTaskAction -Execute    '%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe' -Argument `
    -Command "& 'C:\Location\testing.ps1' -employeeID '$($employeeID)' -employee '$($employee)'"
    Register-ScheduledTask -TaskName $restoreuser -Action $taskAction -Trigger $trigger

I get this result (see image)enter image description here

Any help would be appreciated!

I've tried the approaches here with no luck!

boxdog
  • 7,894
  • 2
  • 18
  • 27
  • I think you need to wrap the whole `-Argument` in `"`. Escape the `"` inside the argument with ` (backtick). – Patrick Nov 09 '22 at 14:13
  • remove the things in "Start in (optional) and add the following in "Add arguments (optional)": ```-NonInteractive -NoLogo -NoProfile -file "C:\Location\testing.ps1"``` – Toni Nov 09 '22 at 15:01
  • Thank you! Wrapping the -Argument worked! I had always tried wrapping it from the -Command. I did try the -file but the task just won't run (I had tried it before), the task exits with 0X01 – sysadmintor Nov 09 '22 at 21:56

0 Answers0