-
Notifications
You must be signed in to change notification settings - Fork 1
/
RunTaskAsUser.ps1
123 lines (107 loc) · 3.64 KB
/
RunTaskAsUser.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<#
.SYNOPSIS
This Script runs a task in the context of another UserName.
.DESCRIPTION
This Script uses the task scheduler to create a new task in the context of specified UserName, runs it immediately after and deletes it in the end.
.PARAMETER UserName
The UserName in whose context the File should be executed.
.PARAMETER PathToFile
The File that is to be executed
.PARAMETER Args
The Arguments that should be passed along to the File.
.PARAMETER Computer
The computer/server this should happen on.
Defaults to localhost.
.INPUTS
See parameters.
.OUTPUTS
None.
.EXAMPLE
PS C:\> & 'C:\TMP\RunTaskAsUserName.ps1' -UserName 'mschoenburg' -PathToFile 'C:\TMP\screenshot.lnk' -Computer 'My-RDS-01'
This runs the link screenshot.lnk in the context of the UserName mschoenburg on the terminal server My-RDS-01.
.EXAMPLE
PS C:\> & 'C:\TMP\RunTaskAsUserName.ps1' -UserName 'mschoenburg' -PathToFile 'C:\TMP\MyProgram.exe'
This would be executed on the local machine.
.EXAMPLE
PS C:\> & 'C:\TMP\RunTaskAsUserName.ps1' -UserName 'mschoenburg' -PathToFile 'C:\Program Files (x86)\Microsoft Office\root\Office16\OUTLOOK.EXE' -Arguments '/safe'
This would start outlook in safe mode.
.NOTES
Author: Michael Schönburg
Current version: 0.1
Last change: 08.06.2021
#>
param (
# The UserName in whose context the File should be executed.
[Parameter(
Mandatory = $true,
Position = 0
)]
[string]
$UserName,
# The File that is to be executed.
[Parameter(
Mandatory = $true,
Position = 1
)]
[string]
$PathToFile,
# The Arguments that should be passed along to the File.
[Parameter(
Mandatory = $false,
Position = 2
)]
[string]
$Arguments,
# The computer/server this should happen on.
[Parameter(
Mandatory = $false
)]
[string]
$Computer = ($env:computername)
)
$ScriptBlock = {
param (
# The UserName in whose context the File should be executed.
[Parameter(
Mandatory = $true,
Position = 0
)]
[string]
$userName,
# The File that is to be executed.
[Parameter(
Mandatory = $true,
Position = 1
)]
[string]
$pathToFile,
# The Arguments that should be passed along to the File.
[Parameter(
Mandatory = $false,
Position = 2
)]
[string]
$arguments
)
#Action
$action = New-ScheduledTaskAction –Execute $PathToFile
if ($arguments.Length -gt 0) {
$action = New-ScheduledTaskAction –Execute $PathToFile -Argument $arguments
}
# Principal
$p = New-ScheduledTaskPrincipal -UserId $UserName -LogonType Interactive -ErrorAction Ignore
# Settings
$s = New-ScheduledTaskSettingsSet -MultipleInstances Parallel -Hidden
# Create the task.
$task = New-ScheduledTask -Action $action -Settings $S -Principal $P
# Unregister the old task if there is one that hasn't been cleaned up.
Unregister-ScheduledTask -TaskName 'TEMPTASK' -ErrorAction Ignore -Confirm:$false
# Register the task.
# Suppressing output via assignment to $null
$null = Register-ScheduledTask -InputObject $task -TaskPath '\KD\' -TaskName 'TEMPTASK'
# Execute the task.
Get-ScheduledTask -TaskName 'TEMPTASK' -TaskPath '\KD\' | Start-ScheduledTask | Out-Null
# Unregister the task.
Unregister-ScheduledTask -TaskName 'TEMPTASK' -ErrorAction Ignore -Confirm:$false
}
Invoke-Command -ComputerName $Computer -ScriptBlock $ScriptBlock -ArgumentList $UserName, $PathToFile, $Arguments