-
Notifications
You must be signed in to change notification settings - Fork 14
/
Import-ScheduledTask.ps1
108 lines (79 loc) · 3.46 KB
/
Import-ScheduledTask.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<#
.SYNOPSIS
This cmdlet is for importing a scheduled task onto local or remote devices
.DESCRIPTION
This uses the Register-ScheduledTask cmdlet to import a scheduled task from a network share into local or remote devices
.PARAMETER Path
Defines the full path and file name definition for the .xml file to import. If remote devices are being used this files location should be a share accessible by them
.PARAMETER TaskName
Define a display name for the task once imported
.PARAMETER TaskPath
Define the path the task should be imported to. Default value is the root directory \
.PARAMETER User
Define a user the task should run as. The default value is the SYSTEM user.
.PARAMETER ComputerName
Indicates the device(s) that should have the scheduled task file imported
.EXAMPLE
Import-ScheduledTask -Path C:\Windows\Temp\TaskImportName.xml -TaskName "My Task" -TaskPath "\" -User SYSTEM
# This example imports a scheduled task xml file and creates a task on the local device
.EXAMPLE
Import-ScheduledTask -Path C:\Windows\Temp\TaskImportName.xml -TaskName "My Task" -TaskPath "\" -User SYSTEM -ComputerName 'DC01.osbornepro.com', '10.0.1.1'
# This example imports a scheduled task xml file and creates a task on DC01.domain.com and 10.0.1.1
.INPUTS
System.String System.Array
.OUTPUTS
Microsoft.Management.Infrastructure.CimInstance#MSFT_ScheduledTask
.NOTES
Author: Robert H. Osborne
Alias: tobor
Contact: [email protected]
.LINK
https://osbornepro.com
https://writeups.osbornepro.com
https://btpssecpack.osbornepro.com
https://github.com/tobor88
https://gitlab.com/tobor88
https://www.powershellgallery.com/profiles/tobor
https://www.linkedin.com/in/roberthosborne/
https://www.credly.com/users/roberthosborne/badges
https://www.hackthebox.eu/profile/52286
#>
Function Import-ScheduledTask {
[CmdletBinding()]
param(
[Parameter(
Position=0,
Mandatory=$True,
ValueFromPipeline=$False,
HelpMessage="`n[H] Define the full path and file name to the task xml file you exported from task scheduler. This can be a network location`n[E] EXAMPLE: C:\Temp\taskfile.xml")] # End Parameter
[Alias('FilePath')]
[String]$Path,
[Parameter(
Position=1,
Mandatory=$True,
ValueFromPipeline=$False,
HelpMessage="`n[H] Define a display name for the task `n[E] EXAMPLE: 'Run Program At Startup'")] # End Parameter
[String]$TaskName,
[Parameter(
Position=2,
Mandatory=$False,
ValueFromPipeline=$False)] # End Parameter
[String]$TaskPath = "\",
[Parameter(
Position=3,
Mandatory=$False,
ValueFromPipeline=$False)] # End Parameter
[String]$User = 'NT AUTHORITY\SYSTEM',
[Parameter(
Mandatory=$False,
ValueFromPipeline=$True,
ValueFromPipelineByPropertyName=$True)] # End Parameter
[Alias('cn')]
[String[]]$ComputerName = $env:COMPUTERNAME
) # End param
$Xml = Get-Content -Path $Path | Out-String
ForEach ($C in $ComputerName) {
Write-Verbose "Creating task $TaskName on $C in the task location $TaskPath"
Register-ScheduledTask -Xml $Xml -TaskName $TaskName -TaskPath $TaskPath -User $User –Force
} # End ForEach
} # End Function Import-ScheduledTask