0

I want to use this powershell command to register a task in task scheduler in my .bat file. I have lots of other commands as well. I want to keep everything to one batch file and not create a .ps1 script specifically for this powershell command. HOWEVER, this powershell does NOT work in batch file. What is wrong with it.

This is what the error says : Register-ScheduledTask : The parameter is incorrect. At line:1 char:4

This is the command IN the batch file:

powershell -command " &{Register-ScheduledTask -Xml (get-content "C:\Users\Disables_Updates.xml" | out-string) -TaskName "\Disables_Updates" -User $env:USERNAME –Force}"
derekbaker783
  • 8,109
  • 4
  • 36
  • 50
zexal985236
  • 33
  • 1
  • 9
  • `powershell -command "& {Register-ScheduledTask -Xml $(get-content 'C:\Users\Disables_Updates.xml' | out-string) -TaskName '\Disables_Updates' -User $env:USERNAME –Force}"` see [this answer](https://stackoverflow.com/a/36143995/3439404) – JosefZ May 03 '20 at 18:03
  • Scheduled tasks are just xml files created on one host, can be exported form that hos, then copied and pasted to any target and directly imported. – postanote May 04 '20 at 03:17

2 Answers2

0

Doublequotes inside doublequotes don't normally work. Take out the inner ones, that you don't need anyway. You probably want to set the task to run as the group "Users", and then export the xml, if you want all users to run it. -force is only needed if you want to overwrite the task. The xml has to be piped in as a single string with the -raw option.

powershell "get-content -raw c:\users\disables_updates.xml | Register-ScheduledTask \Disables_Updates –Force"
js2010
  • 23,033
  • 6
  • 64
  • 66
  • It works for me. What does the xml look like? Can you add that to the question? – js2010 May 03 '20 at 17:28
  • Ok, it runs at system startup as the system user, or the current user if you use $env:username. It works for me. I don't know what else to tell you. – js2010 May 03 '20 at 20:21
0

As per my comment, these are just text files, and export/import from source to a destination should be the most direct for you. No real need to mess with the raw XML of the file in most cases.

Just create a .ps1 and call the .ps1 from a simple batch file vs trying to pass a bunch of command-line level stuff in a string that has to be properly quoted, etc.

Get-ChildItem -Path 'C:\Windows\System32\Tasks'

Export-ScheduledTask 'TestTask' | 
out-file '\\TargetServer\c$\public\TestTask.xml'

Invoke-Command -ComputerName 'TargetServer' -ScriptBlock {
    Register-ScheduledTask -Xml (Get-Content 'C:\Users\public\TestTask.xml' | out-string) -TaskName 'TestTask'
}


# Messing with the XML

# Create your task 
$A = New-ScheduledTaskAction –Execute 'powershell' -Argument 'Hello, from task scheduler'
$T = New-ScheduledTaskTrigger -Weekly -WeeksInterval 1 -DaysOfWeek Monday,Tuesday,Wednesday,Thursday,Friday -At 8am
$S = New-ScheduledTaskSettingsSet
$D = New-ScheduledTask -Action $A -Trigger $T -Settings $S
$Task = Register-ScheduledTask 'TestTask' -InputObject $D



# View the created task XML
Get-Content -Path 'C:\Windows\System32\Tasks\TestTask'


# capture the task to work with
$Task = Get-ScheduledTask -TaskName 'TestTask' 



# Step through the task information.
$Task | Select *


State                 : Ready
Actions               : {MSFT_TaskExecAction}
Author                : 
Date                  : 
Description           : 
Documentation         : 
Principal             : MSFT_TaskPrincipal2
SecurityDescriptor    : 
Settings              : MSFT_TaskSettings3
Source                : 
TaskName              : TestTask
TaskPath              : \
Triggers              : {MSFT_TaskWeeklyTrigger}
URI                   : 
Version               : 
PSComputerName        : 
CimClass              : Root/Microsoft/Windows/TaskScheduler:MSFT_ScheduledTask
CimInstanceProperties : {Actions, Author, Date, Description...}
CimSystemProperties   : Microsoft.Management.Infrastructure.CimSystemProperties


$ScheduleTaskKeys = 
'State',
'Actions',
'Author', 
'Date',
'Description',
'Documentation',
'Principal',
'SecurityDescriptor',
'Settings',
'Source',
'TaskName',
'TaskPath',
'Triggers',
'URI',
'Version',
'PSComputerName'

ForEach($TaskKey in $ScheduleTaskKeys)
{$Task.$TaskKey | Format-List -Force}

# View as JSON
$Task | ConvertTo-Json


# Example XML config
# Configuring triggers
$Task.Triggers | Format-List -Force


Enabled            : True
EndBoundary        : 
ExecutionTimeLimit : 
Id                 : 
Repetition         : MSFT_TaskRepetitionPattern
StartBoundary      : 2018-11-10T08:00:00
DaysOfWeek         : 62
RandomDelay        : P0DT0H0M0S
WeeksInterval      : 1
PSComputerName     :  




$Task.Triggers.Repetition | Format-List * -Force


Duration              : 
Interval              : 
StopAtDurationEnd     : False
PSComputerName        : 
CimClass              : Root/Microsoft/Windows/TaskScheduler:MSFT_TaskRepetitionPattern
CimInstanceProperties : {Duration, Interval, StopAtDurationEnd}
CimSystemProperties   : Microsoft.Management.In




# Modify the trigger repetition settings, which cannot be done via the native cmdlet
$Task.Triggers.Repetition.Duration = 'P1D'
$Task.Triggers.Repetition.Interval = 'PT60M'
$Task | Set-ScheduledTask -User $env:USERNAME

TaskPath   TaskName   State
--------   --------   -----
\          TestTask   Ready



# View the change
$Task.Triggers.Repetition | Format-List * -Force

Duration              : P1D
Interval              : PT60M
StopAtDurationEnd     : False
PSComputerName        : 
CimClass              : Root/Microsoft/Windows/TaskScheduler:MSFT_TaskRepetitionPattern
CimInstanceProperties : {Duration, Interval, StopAtDurationEnd}
CimSystemProperties   : Microsoft.Management.Infrastructure.CimSystemProperties

# Modify the XML file directly – say the repetition times settings using a simple replace, to something else
(Get-Content -Path ‘C:\Windows\System32\Tasks\TestTask’).Replace(‘P1D’,’P12H’) | 
Set-Content -Path ‘C:\Windows\System32\Tasks\TestTask’
postanote
  • 15,138
  • 2
  • 14
  • 25
  • How do you add a trigger for 'On Workstation Unlock' ? and it runs every few minutes indefinitely – zexal985236 May 06 '20 at 03:24
  • You can't add a new trigger to an executing ST. An ST can have multiple triggers, though that effort is time-based: [Task Triggers](https://learn.microsoft.com/en-us/windows/win32/taskschd/task-triggers), [Adding a second trigger in task schedule](https://stackoverflow.com/questions/39290601/adding-a-second-trigger-in-task-schedule). However, Windows proper Startup/Logon/Unlock are all differnt event actions, sooo..... There's that. – postanote May 06 '20 at 03:36
  • I see you posted this as a separate question --- https://stackoverflow.com/questions/61644860/powershell-syntax-for-running-task-every-5-minutes-indefinitely-after-on-worksta --- and you've already gotten an answer from Mike Shepard. – postanote May 06 '20 at 21:23