| « Rename HyperV export for import (clone) | Function IndexOfArray » |
Because I encountered a lot of strugle to create some scheduled tasks on a Windows 2003 machine with PowerShell v2.0.
I used the function of Hugo Peeters (see the link) and then I encountered a problem to pass a task with a parameter between double quotes.
Create-ScheduledTask -ComputerName $Server -TaskName '`"Name`"' -TaskRun '`"C:\WINDOWS\system32\cscript.exe \`"C:\test script\script.vbs\`"`"' -Schedule "DAILY" -StartTime "08:00" -RunAsUser "$domain\$User" -RunAsPwd $Password -Days "*"
This is the only format I used that worked to create a scheduled task to start a vbscript or another task including a parameter. Follow up:
[codespan]
# -----------------------------------------------------
# Creates a scheduled task
Function Create-ScheduledTask
{
# '@ http://www.peetersonline.nl/index.php/powershell/managing-scheduled-tasks-remotely-using-powershell/ '@
param(
[string]$ComputerName = "localhost",
[string]$RunAsUser = "System",
[string]$RunAsPwd = "",
[string]$TaskName = "MyTask",
[string]$TaskRun = '"C:\Program Files\Scripts\Script.vbs"',
[string]$Schedule = "Monthly",
[string]$Modifier = "second",
[string]$Days = "SUN",
[string]$Months = '"MAR,JUN,SEP,DEC"',
[string]$StartTime = "13:00",
[string]$EndTime = "17:00",
[string]$Interval = "60"
)
#$Command = "schtasks.exe /create /s $ComputerName /ru $RunAsUser /tn $TaskName /tr $TaskRun /sc $Schedule /mo $Modifier /d $Days /m $Months /st $StartTime /et $EndTime /ri $Interval /F"
$Command = "schtasks.exe /create /s $ComputerName /ru $RunAsUser /rp $RunAsPwd /tn $TaskName /tr $TaskRun /sc $Schedule /st $StartTime /F"
Invoke-Expression $Command
Clear-Variable Command -ErrorAction SilentlyContinue
}
# -----------------------------------------------------
Create-ScheduledTask -ComputerName $Server -TaskName '`"Name`"' -TaskRun '`"C:\WINDOWS\system32\cscript.exe \`"C:\test script\script.vbs\`"`"' -Schedule "DAILY" -StartTime "08:00" -RunAsUser "$domain\$User" -RunAsPwd $Password -Days "*"
[/codespan]
3 comments
Comments are closed for this post.
$dq = "`""
$quote = $dq + "Double quotes will appear around this text." + $dq
Replace the long -TaskRun parameter with ($quote).
-Thanks