I'm trying to run a simple PowerShell script invoked from a .bat file. The script's purpose is to update the properties of an existing Windows Task Scheduler task so it will run at a time specified by the .bat file and will wake the computer to run the task. (The .bat can't do this itself using the SCHTASKS.exe command because SCHTASKS won't properly set the computer to wake, even if the Wake condition was already set in the task. I presume Windows Task Scheduler isn't smart enough to recognize the need to program the motherboard's RTC when SCHTASKS updates the task's trigger time.) The PowerShell script runs but fails and gives the error messages in my question.
I googled the error message and only found unhelpful references to Azure, which is irrelevant to me since I'm just using a plain Windows 10 Home pc.
I'm new to PowerShell. (I've used .bat a lot.) Here's the PowerShell script:
$TName = $args[0]$TTime = $args[1]$TTrig = New-ScheduledTaskTrigger -Once -At $TTime$Settings = New-ScheduledTaskSettingsSet -Hidden -WakeToRunSet-ScheduledTask -TaskName $TName -Trigger $TTrig -Settings $Settings
The 3rd and 4th lines each give the error message contained in my question. (The 5th line gives an error message about a problem with the -Trigger parameter, but this isn't surprising given that line 3 failed.)
If I paste similar lines one at a time manually into the PowerShell ISE gui (run as Administrator), I don't get error messages and it will update the task.
Here's the .bat line that invokes the PowerShell script to run with Administrator privileges:
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File """"%WAKESCRIPT%"""" """"%WAKETASK%"""" """"%WAKETIME%"""" ' -Verb RunAs}"
where WAKESCRIPT is the path and name of the PowerShell script, WAKETASK is the name of the Windows Task Scheduler task to be modified, and WAKETIME is just a constant string 14:00 that I'm using during this debugging. I learned the basics of this .bat line at:https://blog.danskingdom.com/allow-others-to-run-your-powershell-scripts-from-a-batch-file-they-will-love-you-for-it/
Can anyone help me solve this problem? Thanks in advance.