Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

1 Answer 1

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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