Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need create schedule task programmatically using Powershell (PS remoting).

My task executes a powershell script (and other cases a script cmd) immediately.

$powershellcmd = (get-command powershell.exe).Definition
$upgradeWSPps1 = (Join-Path $ScriptDirectory Tests.LifeCycle.Deploy.MOSS.UpgradeWSP.ps1)

$cmdToRun = "$powershellcmd -ExecutionPolicy Bypass -WindowStyle Hidden -NoLogo -File ""$upgradeWSPps1"" "

If you're creating a scheduled task by providing $cmdToRun to schtasks then you'll need to use both Windows CLI style escape character and Powershell style escape characters for double-quotes. In Powershell the back-tick ` is the escape character, but in Windows CLI (classic Windows shell) it is the forward slash.

Any suggestions about it ? any good sample code ?

share|improve this question

Why would you want to create a scheduled task on a remote computer to immediately run a script? Just run the script directly on the remote computer.

Invoke-Command -ComputerName COMPUTER -ScriptBlock { COMMAND }
share|improve this answer
    
I need it, another issues in my company (permissions in deploy sharepoint, etc). I want testing create scheduled task, I need do it that way. – Kiquenet Sep 10 '12 at 10:55
    
The command to create scheduled tasks is schtasks. – Ansgar Wiechers Sep 10 '12 at 12:21
    
If you're creating a scheduled task by providing $cmdToRun to schtasks then you'll need to use both Windows CLI style escape character and Powershell style escape characters for double-quotes. In Powershell the back-tick ` is the escape character, but in Windows CLI (classic Windows shell) it is the forward slash. Any good sample about it? – Kiquenet Sep 11 '12 at 6:26
    
The escape character for CMD is the caret ^ (and sometimes the backslash), not the forward slash. – Ansgar Wiechers Sep 11 '12 at 18:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .