Create Shedule Task for execute Powershell script immediately using Powershell

1

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 ?

Kiquenet

Posted 2012-09-10T09:48:13.690

Reputation: 269

Answers

0

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 }

Ansgar Wiechers

Posted 2012-09-10T09:48:13.690

Reputation: 4 860

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 – 2012-09-10T10:55:01.613

The command to create scheduled tasks is schtasks.

– Ansgar Wiechers – 2012-09-10T12:21:18.400

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 – 2012-09-11T06:26:09.423

The escape character for CMD is the caret ^ (and sometimes the backslash), not the forward slash. – Ansgar Wiechers – 2012-09-11T18:26:47.837