forked from demisto/content-external-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonServerPowerShell.ps1
More file actions
385 lines (326 loc) · 11.1 KB
/
CommonServerPowerShell.ps1
File metadata and controls
385 lines (326 loc) · 11.1 KB
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
. $PSScriptRoot\demistomock.ps1
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidGlobalVars", "", Scope = "", Justification = "Use of globals set by the Demisto Server")]
# Silence Progress STDOUT (e.g. long http request download progress)
$progressPreference = 'silentlyContinue'
enum ServerLogLevel {
debug
info
error
}
enum EntryTypes {
note = 1
downloadAgent = 2
file = 3
error = 4
pinned = 5
userManagement = 6
image = 7
playgroundError = 8
entryInfoFile = 9
warning = 11
map = 15
widget = 17
}
enum EntryFormats {
html
table
json
text
dbotResponse
markdown
}
# Demist Object Class for communicating with the Demisto Server
[System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidGlobalVars', '', Justification = 'use of global:DemistoServerRequest')]
class DemistoObject {
hidden [hashtable] $ServerEntry
hidden [bool] $IsDebug
hidden [bool] $IsIntegration
hidden [hashtable] $ContextArgs
DemistoObject () {
if($global:InnerContext.GetType().Name -eq 'String') {
$context = $global:InnerContext | ConvertFrom-Json -AsHashtable
}
else {
$context = $global:InnerContext
}
$this.ServerEntry = $context
$this.ContextArgs = $context.args
$this.IsDebug = $context.IsDebug
if ($this.IsDebug) {
$this.ContextArgs.Remove("debug-mode")
}
$this.IsIntegration = $context.integration
}
[hashtable] Args () {
return $this.ContextArgs
}
[hashtable] Params () {
return $this.ServerEntry.params
}
[string] GetCommand () {
return $this.ServerEntry.command
}
[hashtable] Investigation () {
return $this.ServerEntry.context.Inv
}
[hashtable] GetContext () {
return $this.ServerEntry.context.ExecutionContext
}
Log ($Entry) {
$Command = @{ type = "entryLog"; args = @{ message = $Entry } } | ConvertTo-Json -Compress -Depth 20
$global:DemistoOutputStream.WriteLine($Command)
}
Results ($EntryRaw) {
$Entry = $this.ConvertToEntry($EntryRaw)
$Command = @{ type = "result"; results = $Entry } | ConvertTo-Json -Compress -Depth 20
$global:DemistoOutputStream.WriteLine($Command)
}
[array] ConvertToEntry ($EntryRaw) {
$Entry = ""
$EntryType = $EntryRaw.GetType().name
switch ($EntryType) {
"Hashtable" {
if ($EntryRaw.ContainsKey("Contents") -and $EntryRaw.ContainsKey("ContentsFormat")) {
$Entry = @($EntryRaw)
}
else {
$Entry = @(@{Type = 1; Contents = $EntryRaw; ContentsFormat = "json" })
}
; Break
}
"Object[]" {
foreach ($EntryObj in $EntryRaw) {
if (!$Entry) {
$Entry = $this.ConvertToEntry($EntryObj)
}
else {
$Entry += $this.ConvertToEntry($EntryObj)[0]
}
}
; Break
}
"PSCustomObject" {
if ($EntryRaw.Contents -and $EntryRaw.ContentsFormat) {
$Entry = @($EntryRaw)
}
else {
$Entry = @(@{Type = 1; Contents = $EntryRaw; ContentsFormat = "json" })
}
; Break
}
default {
$EntryContent = [string]$EntryRaw
$Entry = @(@{Type = 1; Contents = $EntryContent; ContentsFormat = "text" })
; Break
}
}
return $entry
}
[array] ServerRequest ($Cmd) {
return global:DemistoServerRequest $cmd
}
[array] DT ($Value, $Name) {
return $this.ServerRequest(@{type = "dt"; name = $Name; value = $Value }).result
}
[array] GetFilePath ($ID) {
return $this.ServerRequest(@{type = "getFileByEntryID"; command = "getFilePath"; args = @{id = $ID } })
}
[array] DemistoUrls () {
return $this.ServerRequest(@{type = "demistoUrls" })
}
[array] DemistoVersion () {
return $this.ServerRequest(@{type = "demistoVersion" })
}
[string] UniqueFile () {
return New-Guid
}
[hashtable] ParentEntry () {
return $this.ServerEntry.context.ParentEntry
}
[string] GetLicenseID () {
return $this.ServerRequest(@{type = "executeCommand"; command = "getLicenseID"; args = @{ } }).id
}
Info ($Log) {
DemistoServerLog ([ServerLogLevel]::info) $Log
}
Debug ($Log) {
DemistoServerLog ([ServerLogLevel]::debug) $Log # disable-secrets-detection
}
Error ($Log) {
DemistoServerLog ([ServerLogLevel]::error) $Log
}
[array] GetIncidents () {
return $this.ServerEntry.context.Incidents
}
[array] Incidents () {
return $this.GetIncidents()
}
[hashtable] Incident () {
return $this.GetIncidents()[0]
}
# Script Functions
[array] ExecuteCommand ($Command, $CmdArgs) {
if ( $this.IsIntegration ) {
throw "Method not supported"
}
return $this.ServerRequest(@{type = "executeCommand"; command = $Command; args = $CmdArgs })
}
[array] Execute ($Module, $Command, $CmdArgs) {
if ( $this.IsIntegration ) {
throw "Method not supported"
}
return $this.ServerRequest(@{type = "execute"; module = $Module; command = $Command; args = $CmdArgs })
}
[array] GetAllSupportedCommands () {
if ( $this.IsIntegration ) {
throw "Method not supported"
}
return $this.ServerRequest(@{type = "getAllModulesSupportedCmds" })
}
SetContext ($Name, $Value) {
if ( $this.IsIntegration ) {
throw "Method not supported"
}
$this.ServerRequest(@{type = "setContext"; name = $Name; value = $Value })
}
[Object[]] GetModules () {
if ( $this.IsIntegration ) {
throw "Method not supported"
}
return $this.ServerRequest(@{type = "getAllModules" })
}
# Integration Functions
[string] IntegrationInstance () {
if ( -not $this.IsIntegration ) {
throw "Method not supported"
}
return $this.ServerEntry.context.IntegrationInstance
}
[array] Incidents ($Incidents) {
if ( -not $this.IsIntegration ) {
throw "Method not supported"
}
$Incidents = $Incidents | ConvertTo-Json -Depth 6
return $this.Results(@{Type = 1; Contents = $Incidents; ContentsFormat = "json" })
}
[array] Credentials ($Crds) {
if ( -not $this.IsIntegration ) {
throw "Method not supported"
}
$Credentials = $Crds | ConvertTo-Json -Depth 6
return $this.Results(@{Type = 1; Contents = $Credentials; ContentsFormat = "json" })
}
[Object[]] GetLastRun () {
if ( -not $this.IsIntegration ) {
throw "Method not supported"
}
return $this.ServerRequest(@{type = "executeCommand"; command = "getLastRun"; args = @{ } })
}
SetLastRun ($Value) {
if ( -not $this.IsIntegration ) {
throw "Method not supported"
}
$this.ServerRequest(@{type = "executeCommand"; command = "setLastRun"; args = @{ value = $Value } })
}
[Object[]] GetIntegrationContext () {
if ( -not $this.IsIntegration ) {
throw "Method not supported"
}
return $this.ServerRequest(@{type = "executeCommand"; command = "getIntegrationContext"; args = @{ } })
}
SetIntegrationContext ($Value) {
if ( -not $this.IsIntegration ) {
throw "Method not supported"
}
$this.ServerRequest(@{type = "executeCommand"; command = "setIntegrationContext"; args = @{ value = $Value } })
}
}
[DemistoObject]$demisto = [DemistoObject]::New()
function global:Write-Host($UserInput) { $demisto.Log($UserInput) | Out-Null }
function global:Write-Output($UserInput) { $demisto.Log($UserInput) | Out-Null }
# -----------------------------------------------------------------------
# Utility Functions
# -----------------------------------------------------------------------
<#
.DESCRIPTION
Converts a string representation of args to a list
.PARAMETER Arg
The argument to convert
.PARAMETER Seperator
The seperator to use (default ',')
.OUTPUTS
Object[]. Returns an array of the arguments
#>
function ArgToList($Arg, [string]$Seperator = ",") {
if (! $Arg) {
$r = @()
}
elseif ($Arg.GetType().IsArray) {
$r = $Arg
}
elseif ($Arg[0] -eq "[" -and $Arg[-1] -eq "]") {
# json string
$r = $Arg | ConvertFrom-Json -AsHashtable
}
else {
$r = $Arg.Split($Seperator)
}
# we want to return an array and avoid one-at-a-time processing
# see: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_return?view=powershell-6#return-values-and-the-pipeline
return @(, $r)
}
<#
.DESCRIPTION
Return an Error entry back to the Server
.PARAMETER Message
Messsage to display in the error entry
.PARAMETER Err
Error to log (optional)
.PARAMETER Outputs
The outputs that will be returned to playbook/investigation context (optional)
.OUTPUTS
The error entry object returned to the server
#>
function ReturnError([string]$Message, $Err, [hashtable]$Outputs) {
$demisto.Error("$Message")
if ($Err) {
$errMsg = $Err | Out-String
$demisto.Error($errMsg)
}
$entry = @{Type = [EntryTypes]::error; ContentsFormat = [EntryFormats]::text.ToString(); Contents = $message; EntryContext = $Outputs }
$demisto.Results($entry) | Out-Null
return $entry
}
<#
.DESCRIPTION
This function wraps the $demisto.results(), makes the usage of returning results to the user more intuitively.
.PARAMETER ReadableOutput
Markdown string that will be presented in the warroom, should be human readable
.PARAMETER Outputs
The outputs that will be returned to playbook/investigation context (optional)
.PARAMETER RawResponse
If not provided then will be equal to outputs. usually is the original
raw response from the 3rd party service (optional)
.OUTPUTS
The entry object returned to the server
#>
function ReturnOutputs([string]$ReadableOutput, [hashtable]$Outputs, [hashtable]$RawResponse) {
$entry = @{
Type = [EntryTypes]::note;
ContentsFormat = [EntryFormats]::json.ToString();
HumanReadable = $ReadableOutput;
Contents = $RawResponse;
EntryContext = $Outputs
}
# Return 'readable_output' only if needed
if ($ReadableOutput -and -not $outputs -and -not $RawResponse) {
$entry.Contents = $ReadableOutput
$entry.ContentsFormat = [EntryFormats]::text.ToString();
}
elseif ($Outputs -and -not $RawResponse) {
# if RawResponse was not provided but outputs were provided then set Contents as outputs
$entry.Contents = $Outputs
}
$demisto.Results($entry) | Out-Null
return $entry
}