1

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 -WakeToRun
Set-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.

2
  • Sure, you can use a .bat, but why? You can just create a .ps1, create a shortcut for a user to use. Double-click to run. Either way, you are sending to files to the user; a .bat and .pss1 or .lnk and .ps1. You could also just create a script, use PS2EXE and send one file, double-click to run. Scripts can be set to auto elevate.
    – postanote
    Nov 16 '20 at 18:52
  • @postanote: As I wrote, I know batch language well, and the .bat does other work besides the line I pasted, so there would be a learning curve before I could convert the entire .bat to powershell code, and I expect there would be a lot of issues along the way, like the one I described above. The ultimate goal is to automatically run the .bat unattended in the middle of the night, about once every few days, to put the pc to sleep for about 10 seconds. Putting the pc to sleep causes the ssd to power cycle, which helps mitigate the Crucial ssd's excess write amplification bug for a few days. Nov 17 '20 at 12:44
0

When messing with ST via PowerShell, there are steps that must be taken for the goal needed. Read the details of the posted code using this step-by-step article on the topic.

use PowerShell to create a scheduled task

Creating a Task Action

# Get-LatestAppLog.ps1

## Define the log file
$logDir = 'c:\temp'
$logFile = "$logDir\AppLog_$(Get-Date -format 'yyyy-mm-dd_hh-mm-ss-tt').xml"

## Get the ten latest application log events
Get-WinEvent -LogName application -MaxEvents 10 |
Export-CliXml $logFile -Force

# Create a new task action
$taskAction = New-ScheduledTaskAction `
    -Execute 'powershell.exe' `
    -Argument '-File C:\scripts\Get-LatestAppLog.ps1'
$taskAction


# Create a new trigger (Daily at 3 AM)
$taskTrigger = New-ScheduledTaskTrigger -Daily -At 3PM
$tasktrigger

Register the new PowerShell scheduled task

# The name of your scheduled task.
$taskName = "ExportAppLog"

# Describe the scheduled task.
$description = "Export the 10 newest events in the application log"

# Register the scheduled task
Register-ScheduledTask `
    -TaskName $taskName `
    -Action $taskAction `
    -Trigger $taskTrigger `
    -Description $description

Running the Task

Get-ScheduledTaskInfo -TaskName ExportAppLog
Start-ScheduledTask -TaskName ExportAppLog

# Set the task principal's user ID and run level.
$taskPrincipal = New-ScheduledTaskPrincipal -UserId 'DEVPC\svcTask' -RunLevel Highest
# Set the task compatibility value to Windows 10.

$taskSettings = New-ScheduledTaskSettingsSet -Compatibility Win8

# Update the task principal settings
Set-ScheduledTask -TaskName 'ExportAppLog' -Principal $taskPrincipal -Settings $taskSettings

# Update the task user account and password
Set-ScheduledTask -TaskName 'ExportAppLog' -User $taskPrincipal.UserID -Password 'PASSWORD'

Changing the Trigger

$taskTrigger1 = New-ScheduledTaskTrigger -Daily -At 4:30PM
$taskTrigger2 = New-ScheduledTaskTrigger -Daily -At 1:00AM
Set-ScheduledTask -TaskName 'ExportAppLog' -Trigger $taskTrigger1,$taskTrigger2 -User 'DEVPC\svcTask' -Password 'PASSWORD'

Backing Up a Scheduled Task

# Export the scheduled task object to XML
Get-ScheduledTask -TaskName 'ExportAppLog' | Export-Clixml c:\temp\ExportAppLog.xml

Deleting a Scheduled Task

# Unregister the scheduled task
Unregister-ScheduledTask -TaskName 'ExportAppLog' -Confirm:$false
Get-ScheduledTask -TaskName 'ExportAppLog'

Restoring a Scheduled Task

# Import the Schedule Task backup
$stBackup = Import-Clixml -Path c:\temp\ExportAppLog.xml



# Reset the logon type to "Run only when the user is logged on."
$stBackup.Principal.LogonType = 'Interactive'


# Create a new Scheduled Task object using the imported values
$restoreTask = New-ScheduledTask `
    -Action $stBackup.Actions `
    -Trigger $stBackup.Triggers `
    -Settings $stBackup.Settings `
    -Principal $stBackup.Principal

Register-ScheduledTask `
    -TaskName 'ExportAppLog_restored' `
    -InputObject $restoreTask `
    -User 'DEVPC\svcTask' `
    -Password 'PASSWORD'
1
  • The "Changing the Trigger" code looks much like my code. So I think it doesn't explain the error message when it's called by my .bat, nor what I could do to solve it. Perhaps you could elaborate on what I'm doing wrong? Nov 17 '20 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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