How Can I Run schtasks

I am trying to deploy a new scheduled task using PSADT. The deployment is comprised of 3 steps:

Pre-Install

  1. Created New Folder
  2. Copy items from SupportFiles directory to the newly created directory

Installation
Run the following: schtasks /create /tn “Check Camera Driver” /xml “($dirFiles)\Check Camera Driver.xml” /ru SYSTEM

The pre-installation tasks run fine but the command I need to run doesn’t seem to run as there is not error in the PSADT log and no indication it was referenced during the deployment. Also, the task I need doesn’t show in the Scheduled Tasks. Running the item in the Installation section of the Deploy-Application script manually from PS is successful.

Can someone please advise the proper way to run powershell modules/commands in the script?

Here is one way to create Scheduled tasks with PSADT logging:

Write-Log "Create Scheduled Task using built-in Win10 commands..."
[String]$TaskName =		"Check Camera Driver"
[String]$TaskXMLPath = "$dirFiles\Check Camera Driver.xml"
[String]$TaskXmlFileContent = Get-Content $TaskXMLPath
$Error.Clear()
$Results = Register-ScheduledTask -TaskName $TaskName -Xml $TaskXmlFileContent -Force -ErrorAction Continue 2>&1 | Out-String
Write-log "[$Results]"

If you want to place your task into a folder in Task Scheduler, here are the changes:


[String]$TaskPath =		"MyFolderName"
...
$Results = Register-ScheduledTask -TaskName $TaskName -TaskPath "\$TaskPath\" -Xml $TaskXmlFileContent -Force -ErrorAction Continue 2>&1 | Out-String

Hi, what’s the meaning of 2>&1 before the out-string ?

Just like in CMD, it redirects the error stream to the output stream.

In fact, in PowerShell, it should have been *>&1 to redirect ALL streams to the output stream.

If this doesn’t work, I use the Error/warning/output variables for PowerShell commands.

Thank you! I will try this.