Skip to content

Commit bc2eb19

Browse files
committed
feat(live-response): ✨ Added Invoke-MdeMachineLiveResponse function
1 parent a22cd08 commit bc2eb19

File tree

3 files changed

+88
-1
lines changed

3 files changed

+88
-1
lines changed

src/public/Invoke-MdeMachineAntivirusScan.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
Comment to associate with the action.
1919
2020
.PARAMETER scanType
21-
Optional. Defines the type of the Scan. Required. Allowed values are: 'Quick' or 'Full' (default: 'Quick').
21+
Optional. Defines the type of the Scan. Allowed values are: 'Quick' or 'Full' (default: 'Quick').
2222
2323
.EXAMPLE
2424
Invoke-MdeMachineAntivirusScan -id "MACHINE_ID" -comment "Your comment"
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<#
2+
.SYNOPSIS
3+
Runs a sequence of live response commands on a device.
4+
5+
.DESCRIPTION
6+
Runs a sequence of live response commands on a device.
7+
8+
.NOTES
9+
Author: Jan-Henrik Damaschke
10+
11+
.LINK
12+
https://learn.microsoft.com/en-us/microsoft-365/security/defender-endpoint/run-live-response?view=o365-worldwide
13+
14+
.PARAMETER id
15+
Specifies the id of the target MDE machine.
16+
17+
.PARAMETER comment
18+
Comment to associate with the action.
19+
20+
.PARAMETER commands
21+
Array of commands to run. Allowed values are "PutFile", "RunScript", "GetFile". See the reference link for more details on the body.
22+
23+
.EXAMPLE
24+
Invoke-MdeMachineLiveResponse -id "MACHINE_ID" -comment "Your comment" -commands @(@{type = "RunScript"; params = @(@{key = "scriptName"; value = "scriptFile.ps1"}; @{key = "Args"; value = "argument1"})})
25+
26+
.ROLE
27+
@(@{permission = 'Machine.LiveResponse'; permissionType = 'Application'}, @{permission = 'Machine.LiveResponse'; permissionType = 'Delegated'})
28+
#>
29+
30+
function Invoke-MdeMachineLiveResponse {
31+
[CmdletBinding()]
32+
param (
33+
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
34+
[string]
35+
$id,
36+
[Parameter(Mandatory)]
37+
[string]
38+
$comment,
39+
[Parameter(Mandatory)]
40+
[array]
41+
$commands
42+
)
43+
Begin {
44+
if (-not (Test-MdePermissions -functionName $PSCmdlet.CommandRuntime)) {
45+
$requiredRoles = (Get-Help $PSCmdlet.CommandRuntime -Full).role | Invoke-Expression
46+
Throw "Missing required permission(s). Please check if one of these is in current token roles: $($requiredRoles.permission)"
47+
}
48+
}
49+
Process {
50+
return Invoke-RetryRequest -Method Post -Uri "https://api.securitycenter.microsoft.com/api/machines/$id/runliveresponse" -body (ConvertTo-Json -Depth 5 -InputObject @{ Comment = $comment; Commands = $commands })
51+
}
52+
End {}
53+
}
54+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
BeforeAll {
2+
Remove-Module PSMDE -Force -ErrorAction SilentlyContinue
3+
Import-Module (Split-Path $PSCommandPath).replace('tests', 'src').Replace('public', 'PSMDE.psd1')
4+
}
5+
6+
Describe "Invoke-MdeMachineLiveResponse" {
7+
8+
It 'Should have the PSMDE module loaded' {
9+
$module = Get-Module PSMDE
10+
$module | Should -Not -BeNullOrEmpty
11+
}
12+
13+
It 'Should have access to internal functions' {
14+
InModuleScope PSMDE {
15+
$iar = Get-Command Invoke-AzureRequest
16+
$iar | Should -Not -BeNullOrEmpty
17+
}
18+
}
19+
20+
It 'Should correctly create the request uri' {
21+
InModuleScope PSMDE {
22+
Mock Invoke-RetryRequest { return @{uri = $uri; body = $body } }
23+
Mock Test-MdePermissions { return $true }
24+
$id = '12345'
25+
$comment = 'Comment'
26+
$commands = @(@{type = "RunScript"; params = @(@{key = "scriptName"; value = "scriptFile.ps1" }; @{key = "Args"; value = "argument1" }) })
27+
$body = ConvertTo-Json -Depth 5 -InputObject @{comment = $comment; commands = $commands }
28+
$result = Invoke-MdeMachineLiveResponse -id $id -comment $comment -commands $commands
29+
$result.uri | Should -Be "https://api.securitycenter.microsoft.com/api/machines/$id/runliveresponse"
30+
$result.body | Should -Be $body
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)