Skip to content

Instantly share code, notes, and snippets.

@zbalkan
Last active July 9, 2019 07:42
Show Gist options
  • Save zbalkan/b099f52fdac6626ef2dc7480626df8c0 to your computer and use it in GitHub Desktop.
Save zbalkan/b099f52fdac6626ef2dc7480626df8c0 to your computer and use it in GitHub Desktop.
function New-RemoteScheduledTask {
[CmdletBinding()]
param (
# Hostname to create the task on
[string]
$Hostname,
# Credentials for connecting to the host
[System.Management.Automation.PSCredential]
$Credential,
# Task Name
[string]
$Name,
# Task Description (optional)
[string]
$Description,
# Scheduled task action
[CimInstance]
$Action,
# Scheduled task trigger
[CimInstance]
$Trigger,
# Scheduled task principal
[CimInstance]
$Principal
)
process {
Invoke-Command -ComputerName $Hostname -Credential $Credential -ScriptBlock {
Register-ScheduledTask -Action $Action -Trigger $Trigger -Principal $Principal -TaskName $Name -Description $Description
}
}
}
@zbalkan
Copy link
Author

zbalkan commented Jul 8, 2019

Sample usage:

$a = New-ScheduledTaskAction -Execute notepad
$at = (Get-Date).AddMinutes(2)
$t = New-ScheduledTaskTrigger -At $at -Once
$p = New-ScheduledTaskPrincipal -GroupId "BUILTIN\administrators" -RunLevel Highest
$c = Get-Credential
New-RemoteScheduledTask -Hostname foo -Credentials $c -Name "sample" -Description "" -Action $a -Trigger $t -Principal $p

It does not test the hostname and credentials before execution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment