0

I am trying to create a scheduled task that runs when the system starts up. I am creating this scheduled task in PowerShell with the following code.

$Trigger= New-ScheduledTaskTrigger -AtStartup
    
$User= "NT AUTHORITY\SYSTEM"
    
$Action= New-ScheduledTaskAction -Execute $sendEmailCommand -Argument $args
    
Register-ScheduledTask -TaskName "Send Restart Notification" -Trigger $Trigger -User $User -Action $Action -RunLevel Highest –Force`

The following are the variables:

$sendEmailCommand = "C:\Test\Test.exe"
$args = "-m smtp.gmail.com -p 587 -u myemail -p mypassword -t my message -o tls=yes -k MyEmailSubject -g recipientEmailAddr"

However, when I run the above script, I receive the following error.

New-ScheduledTaskAction : Cannot process argument transformation on parameter 'Argument'. Cannot convert value to type 

If I remove the -Argument from the line with New-ScheduledTaskAction, then it runs just fine with no errors and the task appears in Task Scheduler as expected. This is what that line of code looks like:

$Action= New-ScheduledTaskAction -Execute $sendEmailCommand

I have tried various permutations of quotes and no quotes. What am I doing wrong?

1 Answer 1

2

$args is powershell reserved keyword. Use other name for the variable.

More here

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.