Skip to content

Instantly share code, notes, and snippets.

@thed06
Last active October 30, 2018 22:34
Show Gist options
  • Save thed06/80e39240c6d31136f3a6f69b8627f73a to your computer and use it in GitHub Desktop.
Save thed06/80e39240c6d31136f3a6f69b8627f73a to your computer and use it in GitHub Desktop.
Scheduled Tasks in PowerShell Version 2 via Schedule.Service COM Object
function Install-ScheduledTask {
<#
.SYNOPSIS
Install a scheduled task using Schedule.Service COM object.
.DESCRIPTION
This function installs a scheduled task using Schedule.Service COM object.
.PARAMETER TaskPath
String. The path of the task.
.PARAMETER TaskName
String. The name of the task.
.PARAMETER TaskDescription
String. The description of the created task.
.PARAMETER TaskCommand
String. The program to execute.
.PARAMETER TaskArg
String. Program arguments.
.PARAMETER TaskStartTime
String. Execution time.
.PARAMETER Daily
Switch. Run task daily at execution time.
.PARAMETER Startup
Switch. Run task on system startup (Requires Administrative Privileges).
.PARAMETER Logon
Switch. Run task on user logon (Requires Administrative Privileges).
.PARAMETER ComputerName
String. Specify remote system hostname.
.PARAMETER Domain
String. Specify remote system domain name.
.PARAMETER UserName
String. The user account to run the task.
.PARAMETER Password
String. Specify user account password.
.EXAMPLE
Install-ScheduledTask -TaskPath \ -TaskName "Adobe Update" -TaskDescription "Update for adobe" -TaskCommand "calc.exe" -TaskStartTime "2018-09-10T21:53:00" -Daily
.EXAMPLE
Install-ScheduledTask -TaskName "Adobe Update" -TaskDescription "Update for adobe" -TaskCommand "C:\Windows\System32\notepad.exe" -TaskStartTime "2018-09-10T21:53:00" -Startup -UserName "SYSTEM"
.EXAMPLE
Install-ScheduledTask -TaskName "Adobe Update" -TaskDescription "Update for adobe" -TaskCommand "cmd.exe" -TaskArg "/c calc.exe" -TaskStartTime ([datetime]::Now.AddSeconds(5).ToString("yyyy-MM-dd'T'HH:mm:ss")) -Daily
.EXAMPLE
Install-ScheduledTask -TaskName "Adobe Update" -TaskDescription "Update for adobe" -TaskCommand "cmd.exe" -TaskArg "/c calc.exe" -TaskStartTime ([datetime]::Now.AddMinutes(2).ToString("yyyy-MM-dd'T'HH:mm:ss")) -Daily -ComputerName "Target-PC" -Domain "TARGET-PC" -UserName "First.Last" -Password "s3cr3t"
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$False)]
[string]$TaskPath,
[Parameter(Mandatory=$False)]
[string]$TaskName,
[Parameter(Mandatory=$False)]
[string]$TaskDescription,
[Parameter(Mandatory=$False)]
[string]$TaskCommand,
[Parameter(Mandatory=$False)]
[string]$TaskArg,
[Parameter(Mandatory=$False)]
[string]$TaskStartTime,
[Parameter(Mandatory=$False)]
[switch]$Daily = $False,
[Parameter(Mandatory=$False)]
[switch]$Startup = $False,
[Parameter(Mandatory=$False)]
[switch]$Logon = $False,
[Parameter(Mandatory=$False)]
[string]$ComputerName,
[Parameter(Mandatory=$False)]
[string]$Domain,
[Parameter(Mandatory=$False)]
[string]$UserName,
[Parameter(Mandatory=$False)]
[string]$Password
)
$Service = new-object -ComObject("Schedule.Service")
if (!($TaskName) -or !($TaskDescription) -or !($TaskCommand) -or !($TaskStartTime)) {
Write-Host "You must specify all of the following parameters: -TaskName / -TaskDescription / -TaskCommand / -TaskStartTime / [-Daily | -Startup | -Logon]"
return
}
if (($Daily -eq $False) -and ($Startup -eq $False) -and ($Logon -eq $False)) {
Write-Host "You must specify a trigger: -Daily / -Startup / -Logon"
return
}
if (($Daily -and $Startup) -or ($Daily -and $Logon) -or ($Startup -and $Logon)) {
Write-Host "You can only specify one trigger: -Daily / -Startup / -Logon"
return
}
if ($ComputerName) {
if ($Domain -and $UserName -and $Password) {
$Service.Connect($ComputerName,$UserName,$Domain,$Password)
}
elseif ($UserName -and $Password) {
$Service.Connect($ComputerName,$UserName,$null,$Password)
}
else {
$Service.Connect($ComputerName)
}
}
else {
$Service.Connect()
}
if ($TaskPath) {
$TaskFolder = $Service.GetFolder($TaskPath)
}
else {
$TaskFolder = $Service.GetFolder("\")
}
$TaskDefinition = $Service.NewTask(0)
$TaskDefinition.RegistrationInfo.Description = "$TaskDescription"
$TaskDefinition.Settings.Enabled = $true
$TaskDefinition.Settings.AllowDemandStart = $true
$Triggers = $TaskDefinition.Triggers
if ($Daily) {
$Trigger = $Triggers.Create(2) # 2: Time-Based (Standard Privileges)
}
elseif ($Startup) {
$Trigger = $Triggers.Create(8) # On-Boot (Administrative Privileges)
}
elseif ($Logon) {
$Trigger = $Triggers.Create(9) # 9: User-Logon (Administrative Privileges)
}
$Trigger.Enabled = $true
$Trigger.StartBoundary = $TaskStartTime
$Action = $TaskDefinition.Actions.Create(0)
$Action.Path = "$TaskCommand"
if ($TaskArg) {
$Action.Arguments = "$TaskArg"
}
if ($Username) {
$TaskFolder.RegisterTaskDefinition($TaskName, $TaskDefinition, 6, $UserName ,$null , 3)
}
else {
$TaskFolder.RegisterTaskDefinition($TaskName, $TaskDefinition, 6, $null ,$null , 3)
}
}
function Remove-ScheduledTask {
<#
.SYNOPSIS
Remove a scheduled task using Schedule.Service COM object.
.DESCRIPTION
This function removes a scheduled task using Schedule.Service COM object.
.PARAMETER TaskPath
String. The path of the task.
.PARAMETER TaskName
String. The name of the task.
.PARAMETER ComputerName
String. Specify remote system hostname.
.PARAMETER Domain
String. Specify remote system domain name.
.PARAMETER UserName
String. The user account for remote authentication.
.PARAMETER Password
String. Specify user account password.
.EXAMPLE
Remove-ScheduledTask -TaskName "Adobe Update"
.EXAMPLE
Remove-ScheduledTask -TaskName "Adobe Update" -ComputerName "Target-PC" -Domain 'NTDOMAIN' -UserName "First.Last" -Password "s3cr3t"
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$False)]
[string]$TaskPath,
[Parameter(Mandatory=$False)]
[string]$TaskName,
[Parameter(Mandatory=$False)]
[string]$ComputerName,
[Parameter(Mandatory=$False)]
[string]$Domain,
[Parameter(Mandatory=$False)]
[string]$UserName,
[Parameter(Mandatory=$False)]
[string]$Password
)
$Service = new-object -ComObject("Schedule.Service")
if (!($TaskName)) {
Write-Host "You must specify all of the following parameters: -TaskName"
return
}
if ($ComputerName) {
if ($Domain -and $UserName -and $Password) {
$Service.Connect($ComputerName,$UserName,$Domain,$Password)
}
elseif ($UserName -and $Password) {
$Service.Connect($ComputerName,$UserName,$null,$Password)
}
else {
$Service.Connect($ComputerName)
}
}
else {
$Service.Connect()
}
if ($TaskPath) {
$TaskFolder = $Service.GetFolder($TaskPath)
}
else {
$TaskFolder = $Service.GetFolder("\")
}
$TaskFolder.DeleteTask($TaskName, $null)
}
function List-ScheduledTask {
<#
.SYNOPSIS
List scheduled tasks using Schedule.Service COM object.
.DESCRIPTION
This function lists scheduled tasks using Schedule.Service COM object.
.PARAMETER TaskPath
String. The path of the task.
.PARAMETER TaskName
String. The name of the task.
.PARAMETER ComputerName
String. Specify remote system hostname.
.PARAMETER Domain
String. Specify remote system domain name.
.PARAMETER UserName
String. The user account for remote authentication.
.PARAMETER Password
String. Specify user account password.
.EXAMPLE
List-ScheduledTask -TaskPath \
.EXAMPLE
List-ScheduledTask -TaskPath \ -TaskName "Adobe Update" -ComputerName "Target-PC" -Domain 'NTDOMAIN' -UserName "First.Last" -Password "s3cr3t"
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$False)]
[string]$TaskPath,
[Parameter(Mandatory=$False)]
[string]$TaskName,
[Parameter(Mandatory=$False)]
[string]$ComputerName,
[Parameter(Mandatory=$False)]
[string]$Domain,
[Parameter(Mandatory=$False)]
[string]$UserName,
[Parameter(Mandatory=$False)]
[string]$Password
)
$Service = new-object -ComObject("Schedule.Service")
if ($ComputerName) {
if ($Domain -and $UserName -and $Password) {
$Service.Connect($ComputerName,$UserName,$Domain,$Password)
}
elseif ($UserName -and $Password) {
$Service.Connect($ComputerName,$UserName,$null,$Password)
}
else {
$Service.Connect($ComputerName)
}
}
else {
$Service.Connect()
}
$TaskFolder = $Service.GetFolder($TaskPath)
if ($TaskName) {
$TaskFolder.GetTask($TaskName)
}
else {
$TaskFolder.GetTasks(0)
}
}
function Run-ScheduledTask {
<#
.SYNOPSIS
Runs a scheduled task using Schedule.Service COM object.
.DESCRIPTION
This function runs a scheduled task using Schedule.Service COM object.
.PARAMETER TaskPath
String. The path of the task.
.PARAMETER TaskName
String. The name of the task.
.PARAMETER ComputerName
String. Specify remote system hostname.
.PARAMETER Domain
String. Specify remote system domain name.
.PARAMETER UserName
String. The user account for remote authentication.
.PARAMETER Password
String. Specify user account password.
.EXAMPLE
Run-ScheduledTask -TaskPath \ -TaskName "Adobe Update"
.EXAMPLE
Run-ScheduledTask -TaskPath \ -TaskName "Adobe Update" -ComputerName "Target-PC" -Domain 'NTDOMAIN' -UserName "First.Last" -Password "s3cr3t"
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$False)]
[string]$TaskPath,
[Parameter(Mandatory=$False)]
[string]$TaskName,
[Parameter(Mandatory=$False)]
[string]$ComputerName,
[Parameter(Mandatory=$False)]
[string]$Domain,
[Parameter(Mandatory=$False)]
[string]$UserName,
[Parameter(Mandatory=$False)]
[string]$Password
)
$Service = new-object -ComObject("Schedule.Service")
if (!($TaskName)) {
Write-Host "You must specify all of the following parameters: -TaskName"
return
}
if ($ComputerName) {
if ($Domain -and $UserName -and $Password) {
$Service.Connect($ComputerName,$UserName,$Domain,$Password)
}
elseif ($UserName -and $Password) {
$Service.Connect($ComputerName,$UserName,$null,$Password)
}
else {
$Service.Connect($ComputerName)
}
}
else {
$Service.Connect()
}
if ($TaskPath) {
$TaskFolder = $Service.GetFolder($TaskPath)
}
else {
$TaskFolder = $Service.GetFolder("\")
}
$Task = $TaskFolder.GetTask($TaskName)
$Task.Run(0)
}
function Stop-ScheduledTask {
<#
.SYNOPSIS
Stops a scheduled task using Schedule.Service COM object.
.DESCRIPTION
This function Stops a scheduled task using Schedule.Service COM object.
.PARAMETER TaskPath
String. The path of the task.
.PARAMETER TaskName
String. The name of the task.
.PARAMETER ComputerName
String. Specify remote system hostname.
.PARAMETER Domain
String. Specify remote system domain name.
.PARAMETER UserName
String. The user account to run the task.
.PARAMETER Password
String. Specify user account password.
.EXAMPLE
Stop-ScheduledTask -TaskPath \ -TaskName "Adobe Update"
.EXAMPLE
Stop-ScheduledTask -TaskPath \ -TaskName "Adobe Update" -ComputerName "Target-PC" -Domain 'NTDOMAIN' -UserName "First.Last" -Password "s3cr3t"
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$False)]
[string]$TaskPath,
[Parameter(Mandatory=$False)]
[string]$TaskName,
[Parameter(Mandatory=$False)]
[string]$ComputerName,
[Parameter(Mandatory=$False)]
[string]$Domain,
[Parameter(Mandatory=$False)]
[string]$UserName,
[Parameter(Mandatory=$False)]
[string]$Password
)
$Service = new-object -ComObject("Schedule.Service")
if (!($TaskName)) {
Write-Host "You must specify all of the following parameters: -TaskName"
return
}
if ($ComputerName) {
if ($Domain -and $UserName -and $Password) {
$Service.Connect($ComputerName,$UserName,$Domain,$Password)
}
elseif ($UserName -and $Password) {
$Service.Connect($ComputerName,$UserName,$null,$Password)
}
else {
$Service.Connect($ComputerName)
}
}
else {
$Service.Connect()
}
if ($TaskPath) {
$TaskFolder = $Service.GetFolder($TaskPath)
}
else {
$TaskFolder = $Service.GetFolder("\")
}
$Task = $TaskFolder.GetTask($TaskName)
$Task.Stop(0)
}
function Toggle-ScheduledTask {
<#
.SYNOPSIS
Enables / Disables a scheduled task using Schedule.Service COM object.
.DESCRIPTION
This function Enables or Disables a scheduled task using Schedule.Service COM object.
.PARAMETER TaskPath
String. The path of the task.
.PARAMETER TaskName
String. The name of the task.
.PARAMETER ComputerName
String. Specify remote system hostname.
.PARAMETER Domain
String. Specify remote system domain name.
.PARAMETER UserName
String. The user account for remote authentication.
.PARAMETER Password
String. Specify user account password.
.EXAMPLE
Stop-ScheduledTask -TaskPath \ -TaskName "Adobe Update"
.EXAMPLE
Stop-ScheduledTask -TaskPath \ -TaskName "Adobe Update" -ComputerName "Target-PC" -Domain 'NTDOMAIN' -UserName "First.Last" -Password "s3cr3t"
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$False)]
[string]$TaskPath,
[Parameter(Mandatory=$False)]
[string]$TaskName,
[Parameter(Mandatory=$False)]
[switch]$Enable,
[Parameter(Mandatory=$False)]
[switch]$Disable,
[Parameter(Mandatory=$False)]
[string]$ComputerName,
[Parameter(Mandatory=$False)]
[string]$Domain,
[Parameter(Mandatory=$False)]
[string]$UserName,
[Parameter(Mandatory=$False)]
[string]$Password
)
$Service = new-object -ComObject("Schedule.Service")
if (!($TaskName) -or !($Enable -or $Disable)) {
Write-Host "You must specify all of the following parameters: -TaskName / [-Enable | -Disable]"
return
}
if ($ComputerName) {
if ($Domain -and $UserName -and $Password) {
$Service.Connect($ComputerName,$UserName,$Domain,$Password)
}
elseif ($UserName -and $Password) {
$Service.Connect($ComputerName,$UserName,$null,$Password)
}
else {
$Service.Connect($ComputerName)
}
}
else {
$Service.Connect()
}
if ($TaskPath) {
$TaskFolder = $Service.GetFolder($TaskPath)
}
else {
$TaskFolder = $Service.GetFolder("\")
}
$Task = $TaskFolder.GetTask($TaskName)
if ($Enable) {
$Task.Enabled = $true
}
elseif ($Disable) {
$Task.Enabled = $false
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment