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 protected]"
    $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!

3
  • I think you need to wrap the whole -Argument in ". Escape the " inside the argument with ` (backtick). Commented Nov 9, 2022 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" Commented Nov 9, 2022 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 Commented Nov 9, 2022 at 21:56

0

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.