-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSampleConstrainedEndpoint.ps1
368 lines (319 loc) · 9.17 KB
/
SampleConstrainedEndpoint.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# Need to set up a new Event Log source:
# New-EventLog -Source ExampleConstrainedEndpoint -LogName application
$Script:AssumedUser = $PSSenderInfo.UserInfo.Identity.name
if ($Script:AssumedUser) {
Write-EventLog -LogName Application -Source ExampleConstrainedEndpoint -EventId 1 -Message "$Script:AssumedUser, Started a remote Session"
}
#region functions
Function Get-Service {
<#
.FORWARDHELPTARGETNAME Get-Service
#>
[CmdletBinding(DefaultParameterSetName='Default')]
param(
[Parameter(ParameterSetName='Default', Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[Alias('ServiceName')]
[string[]]
${Name},
[Alias('DS')]
[switch]
${DependentServices},
[Alias('SDO','ServicesDependedOn')]
[switch]
${RequiredServices},
[Parameter(ParameterSetName='DisplayName', Mandatory=$true)]
[string[]]
${DisplayName},
[ValidateNotNullOrEmpty()]
[string[]]
${Include},
[ValidateNotNullOrEmpty()]
[string[]]
${Exclude},
[Parameter(ParameterSetName='InputObject', ValueFromPipeline=$true)]
[ValidateNotNullOrEmpty()]
[PSCustomObject[]]
${InputObject}
)
PROCESS {
$params = @{}
ForEach ( $key in $PSBoundParameters.Keys ) {
$params.Add($key,$PSBoundParameters[$key])
}
try {
$service = Microsoft.PowerShell.Management\Get-Service @params -ErrorAction Stop| Where-Object { $_.DisplayName -match "Spooler"}
if ( $null -ne $service ) {
$service
}
else {
if ( $DisplayName -or $Name ) {
throw "Cannot find any service with service name, or you do not have permission."
}
}
}
catch {
throw $_
}
}
}
Function Start-Service {
<#
.FORWARDHELPTARGETNAME Start-Service
#>
[CmdletBinding()]
PARAM(
[Parameter(Mandatory=$False)]
[ValidateScript({
if (-not(((Get-Service).Name) -contains $_) ) {
throw "You do not have permission to do that."
}
else {
return $true
}
})]
[string[]]
[Alias("ServiceName")]
$Name,
[Parameter(Mandatory=$False)]
[switch]
$DependentServices,
[Parameter(Mandatory=$False)]
[switch]
$RequiredServices,
[Parameter(Mandatory=$False)]
[string[]]
[ValidateScript({
if (-not(((Get-Service).DisplayName) -contains $_) ) {
throw "You do not have permission to do that."
}
else {
return $true
}
})]
$DisplayName,
[Parameter(Mandatory=$False)]
[string[]]
$Include,
[Parameter(Mandatory=$False)]
[string[]]
$Exclude,
[Parameter(Mandatory=$False)]
[PSCustomObject[]]
$InputObject,
[Parameter(Mandatory=$False)]
[switch]
$Passthru
)
PROCESS {
$params = @{}
ForEach ( $key in $PSBoundParameters.Keys ) {
if ( $key -eq "Passthru" ) {
continue
}
$params.Add($key,$PSBoundParameters[$key])
}
Microsoft.PowerShell.Management\Start-Service @params
if ($Script:AssumedUser) {
$msg = "{0} Started Service(s): {1}" -f $Script:AssumedUser, ($Service.Name -Join ",")
Write-EventLog -LogName Application -Source ExampleConstrainedEndpoint -EventId 1 -Message $msg
}
$Service = Get-Service @params
if ( $null -ne $Service ) {
$InternalParams = ${
InputObject = $Service
}
if ( $PSBoundParameters.ContainsKey("Passthru") ) {
$InternalParams.Add("Passthru", $Passthru)
}
Microsoft.PowerShell.Management\Start-Service @InternalParams
if ($Script:AssumedUser) {
$msg = "{0} Started Service(s): {1}" -f $Script:AssumedUser, ($Service.Name -Join ",")
Write-EventLog -LogName Application -Source ExampleConstrainedEndpoint -EventId 1 -Message $msg
}
}
else {
throw "Cannot find any service with service name, or you do not have permission."
}
}
}
Function Restart-Service {
<#
.FORWARDHELPTARGETNAME Restart-Service
#>
[CmdletBinding()]
PARAM(
[Parameter(Mandatory=$False)]
[ValidateScript({
if (-not(((Get-Service).Name) -contains $_) ) {
throw "You do not have permission to do that."
}
else {
return $true
}
})]
[string[]]
[Alias("ServiceName")]
$Name,
[Parameter(Mandatory=$False)]
[switch]
$DependentServices,
[Parameter(Mandatory=$False)]
[switch]
$RequiredServices,
[Parameter(Mandatory=$False)]
[string[]]
[ValidateScript({
if (-not(((Get-Service).DisplayName) -contains $_) ) {
throw "You do not have permission to do that."
}
else {
return $true
}
})]
$DisplayName,
[Parameter(Mandatory=$False)]
[string[]]
$Include,
[Parameter(Mandatory=$False)]
[string[]]
$Exclude,
[Parameter(Mandatory=$False)]
[PSCustomObject[]]
$InputObject,
[Parameter(Mandatory=$False)]
[switch]
$Passthru
)
PROCESS {
$params = @{}
ForEach ( $key in $PSBoundParameters.Keys ) {
if ( $key -eq "Passthru" ) {
continue
}
$params.Add($key,$PSBoundParameters[$key])
}
Microsoft.PowerShell.Management\Restart-Service @Params
if ($Script:AssumedUser) {
$msg = "{0} Restarted Service(s): {1}" -f $Script:AssumedUser, ($Service.Name -Join ",")
Write-EventLog -LogName Application -Source ExampleConstrainedEndpoint -EventId 1 -Message $msg
}
$Service = Get-Service @params
if ( $null -ne $Service ) {
$InternalParams = ${
InputObject = $Service
}
if ( $PSBoundParameters.ContainsKey("Passthru") ) {
$InternalParams.Add("Passthru", $Passthru)
}
Microsoft.PowerShell.Management\Restart-Service @InternalParams
if ($Script:AssumedUser) {
$msg = "{0} Restarted Service(s): {1}" -f $Script:AssumedUser, ($Service.Name -Join ",")
Write-EventLog -LogName Application -Source ExampleConstrainedEndpoint -EventId 1 -Message $msg
}
}
else {
throw "Cannot find any service with service name, or you do not have permission."
}
}
}
Function Stop-Service {
<#
.FORWARDHELPTARGETNAME Stop-Service
#>
[CmdletBinding()]
PARAM(
[Parameter(Mandatory=$False)]
[ValidateScript({
if (-not(((Get-Service).Name) -contains $_) ) {
throw "You do not have permission to do that."
}
else {
return $true
}
})]
[string[]]
[Alias("ServiceName")]
$Name,
[Parameter(Mandatory=$False)]
[switch]
$DependentServices,
[Parameter(Mandatory=$False)]
[switch]
$RequiredServices,
[Parameter(Mandatory=$False)]
[string[]]
[ValidateScript({
if (-not(((Get-Service).DisplayName) -contains $_) ) {
throw "You do not have permission to do that."
}
else {
return $true
}
})]
$DisplayName,
[Parameter(Mandatory=$False)]
[string[]]
$Include,
[Parameter(Mandatory=$False)]
[string[]]
$Exclude,
[Parameter(Mandatory=$False)]
[PSCustomObject[]]
$InputObject,
[Parameter(Mandatory=$False)]
[switch]
$Passthru
)
PROCESS {
$params = @{}
ForEach ( $key in $PSBoundParameters.Keys ) {
if ( $key -eq "Passthru" ) {
continue
}
$params.Add($key,$PSBoundParameters[$key])
}
Microsoft.PowerShell.Management\Stop-Service @params
if ($Script:AssumedUser) {
$msg = "{0} Stopped Service(s): {1}" -f $Script:AssumedUser, ($Service.Name -Join ",")
Write-EventLog -LogName Application -Source ExampleConstrainedEndpoint -EventId 1 -Message $msg
}
$Service = Get-Service @params
if ( $null -ne $Service ) {
$InternalParams = ${
InputObject = $Service
}
if ( $PSBoundParameters.ContainsKey("Passthru") ) {
$InternalParams.Add("Passthru", $Passthru)
}
Microsoft.PowerShell.Management\Stop-Service @InternalParams
if ($Script:AssumedUser) {
$msg = "{0} Stopped Service(s): {1}" -f $Script:AssumedUser, ($Service.Name -Join ",")
Write-EventLog -LogName Application -Source ExampleConstrainedEndpoint -EventId 1 -Message $msg
}
}
else {
throw "Cannot find any service with service name, or you do not have permission."
}
}
}
#endregion functions
if (-not $psise) {
# set visibility of all Cmdlets, Filters, Functions, and Aliases to Private. This prevents them being executed outside of a function.
Get-Command -CommandType Cmdlet,Filter | ForEach-Object {$_.Visibility = 'Private' }
Get-Command -CommandType Function |
Where-Object { -NOT(@("Get-Service", "Start-Service", "Restart-Service", "Stop-Service") -contains $_.Name) } |
ForEach-Object {$_.Visibility = 'Private' }
Get-Alias | ForEach-Object {$_.Visibility = 'Private' }
#To show multiple commands put the name as a comma separated list
# Get-Command -Name Get-Printer | ForEach-Object {$_.Visibility = 'Public' }
$ExecutionContext.SessionState.Applications.Clear()
$ExecutionContext.SessionState.Scripts.Clear()
# Expose some commands that will be useful, and don't do anything just in case
$RemoteServer = [System.Management.Automation.Runspaces.InitialSessionState]::CreateRestricted(
[System.Management.Automation.SessionCapabilities]::RemoteServer)
$RemoteServer.Commands.Where{($_.Visibility -eq 'public') -and ($_.CommandType -eq 'Function') } | ForEach-Object {
Set-Item -path "Function:\$($_.Name)" -Value $_.Definition
}
}
# set the Language mode - this prevents the user from defining their own functions and bypassing our security.
if (-not $psise) {$ExecutionContext.SessionState.LanguageMode = [System.Management.Automation.PSLanguageMode]::NoLanguage}