Skip to content

Commit ee561d0

Browse files
committed
feat(stop-machine-action): ✨ Added Stop-MdeMachineAction function
1 parent 6c1e291 commit ee561d0

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/public/Stop-MdeMachineAction.ps1

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<#
2+
.SYNOPSIS
3+
Cancel an already launched machine action.
4+
5+
.DESCRIPTION
6+
Cancel an already launched machine action that is not yet in final state (completed, canceled, failed). The necessary API permission (scope) depends on the type of machine action to be stopped.
7+
8+
.NOTES
9+
Author: Jan-Henrik Damaschke
10+
11+
.LINK
12+
https://learn.microsoft.com/en-us/microsoft-365/security/...
13+
14+
.PARAMETER id
15+
Specifies the id of the target MDE machine action.
16+
17+
.PARAMETER comment
18+
Comment to associate with the cancellation action.
19+
20+
.EXAMPLE
21+
Remove-MdeMachine -id "MACHINE_ACTION_ID" -comment "Your comment"
22+
23+
.ROLE
24+
@(@{permission = 'Machine.CollectForensics'; permissionType = 'Application' }, @{permission = 'Machine.Isolate'; permissionType = 'Application' }, @{permission = 'Machine.RestrictExecution'; permissionType = 'Application' }, @{permission = 'Machine.Scan'; permissionType = 'Application' }, @{permission = 'Machine.Offboard'; permissionType = 'Application' }, @{permission = 'Machine.StopAndQuarantine'; permissionType = 'Application' }, @{permission = 'Machine.LiveResponse'; permissionType = 'Application' }, @{permission = 'Machine.CollectForensics'; permissionType = 'Delegated' },@{permission = 'Machine.Isolate'; permissionType = 'Delegated' },@{permission = 'Machine.RestrictExecution'; permissionType = 'Delegated' },@{permission = 'Machine.Scan'; permissionType = 'Delegated' },@{permission = 'Machine.Offboard'; permissionType = 'Delegated' },@{permission = 'Machine.StopAndQuarantineMachine.LiveResponse'; permissionType = 'Delegated' })
25+
#>
26+
27+
function Stop-MdeMachineAction {
28+
[CmdletBinding()]
29+
param (
30+
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)]
31+
[string]
32+
$id,
33+
[Parameter(Mandatory)]
34+
[string]
35+
$comment
36+
)
37+
Begin {
38+
if (-not (Test-MdePermissions -functionName $PSCmdlet.CommandRuntime)) {
39+
$requiredRoles = (Get-Help $PSCmdlet.CommandRuntime -Full).role | Invoke-Expression
40+
Throw "Missing required permission(s). Please check if one of these is in current token roles: $($requiredRoles.permission)"
41+
}
42+
}
43+
Process {
44+
return Invoke-RetryRequest -Method Post -Uri "https://api.securitycenter.microsoft.com/api/machineactions/$id/cancel" -body (ConvertTo-Json -InputObject @{ Comment = $comment })
45+
}
46+
End {}
47+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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 "Stop-MdeMachineAction" {
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+
$body = ConvertTo-Json -Depth 5 -InputObject @{comment = $comment }
27+
$result = Stop-MdeMachineAction -id $id -comment $comment
28+
$result.uri | Should -Be "https://api.securitycenter.microsoft.com/api/machineactions/$id/cancel"
29+
$result.body | Should -Be $body
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)