-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(machine-offboarding): ✨ Added Remove-MdeMachine function
- Loading branch information
Showing
5 changed files
with
205 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<# | ||
.SYNOPSIS | ||
Offboard device from Defender for Endpoint. | ||
.DESCRIPTION | ||
Offboard device from Defender for Endpoint. | ||
.NOTES | ||
Author: Jan-Henrik Damaschke | ||
.LINK | ||
https://learn.microsoft.com/en-us/microsoft-365/security/defender-endpoint/offboard-machine-api?view=o365-worldwide | ||
.PARAMETER id | ||
Specifies the id of the target MDE machine. | ||
.PARAMETER comment | ||
Comment to associate with the action. | ||
.EXAMPLE | ||
Remove-MdeMachine -id "MACHINE_ID" -comment "Your comment" | ||
.ROLE | ||
@(@{permission = 'Machine.Offboard'; permissionType = 'Application'}, @{permission = 'Machine.Offboard'; permissionType = 'Delegated'}) | ||
#> | ||
|
||
function Remove-MdeMachine { | ||
[CmdletBinding()] | ||
param ( | ||
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ValueFromPipeline)] | ||
[string] | ||
$id, | ||
[Parameter(Mandatory)] | ||
[string] | ||
$comment | ||
) | ||
Begin { | ||
if (-not (Test-MdePermissions -functionName $PSCmdlet.CommandRuntime)) { | ||
$requiredRoles = (Get-Help $PSCmdlet.CommandRuntime -Full).role | Invoke-Expression | ||
Throw "Missing required permission(s). Please check if one of these is in current token roles: $($requiredRoles.permission)" | ||
} | ||
} | ||
Process { | ||
return Invoke-RetryRequest -Method Post -Uri "https://api.securitycenter.microsoft.com/api/machines/$id/offboard" -body (ConvertTo-Json -InputObject @{ Comment = $comment }) | ||
} | ||
End {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
BeforeAll { | ||
Remove-Module PSMDE -Force -ErrorAction SilentlyContinue | ||
Import-Module (Split-Path $PSCommandPath).replace('tests', 'src').Replace('public', 'PSMDE.psd1') | ||
} | ||
|
||
Describe "Remove-MdeMachine" { | ||
|
||
It 'Should have the PSMDE module loaded' { | ||
$module = Get-Module PSMDE | ||
$module | Should -Not -BeNullOrEmpty | ||
} | ||
|
||
It 'Should have access to internal functions' { | ||
InModuleScope PSMDE { | ||
$iar = Get-Command Invoke-AzureRequest | ||
$iar | Should -Not -BeNullOrEmpty | ||
} | ||
} | ||
|
||
It 'Should correctly create the request uri' { | ||
InModuleScope PSMDE { | ||
Mock Invoke-RetryRequest { return @{uri = $uri; body = $body } } | ||
Mock Test-MdePermissions { return $true } | ||
$id = '12345' | ||
$comment = 'Comment' | ||
$body = ConvertTo-Json -Depth 5 -InputObject @{comment = $comment } | ||
$result = Remove-MdeMachine -id $id -comment $comment | ||
$result.uri | Should -Be "https://api.securitycenter.microsoft.com/api/machines/$id/offboard" | ||
$result.body | Should -Be $body | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
BeforeAll { | ||
Remove-Module PSMDE -Force -ErrorAction SilentlyContinue | ||
Import-Module (Split-Path $PSCommandPath).replace('tests', 'src').Replace('public', 'PSMDE.psd1') | ||
} | ||
|
||
Describe "Remove-MdeMachineTag" { | ||
|
||
It 'Should have the PSMDE module loaded' { | ||
$module = Get-Module PSMDE | ||
$module | Should -Not -BeNullOrEmpty | ||
} | ||
|
||
It 'Should have access to internal functions' { | ||
InModuleScope PSMDE { | ||
$iar = Get-Command Invoke-AzureRequest | ||
$iar | Should -Not -BeNullOrEmpty | ||
} | ||
} | ||
|
||
It 'Should correctly create the request uri' { | ||
InModuleScope PSMDE { | ||
Mock Invoke-RetryRequest { return @{uri = $uri; body = $body } } | ||
Mock Test-MdePermissions { return $true } | ||
$id = '12345' | ||
$tag = 'monitored' | ||
$body = ConvertTo-Json -Depth 5 -InputObject @{ Value = $tag; Action = 'Remove' } | ||
$result = Remove-MdeMachineTag -id $id -tag $tag | ||
$result.uri | Should -Be "https://api.securitycenter.microsoft.com/api/machines/$id/tags" | ||
$result.body | Should -Be $body | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
BeforeAll { | ||
Remove-Module PSMDE -Force -ErrorAction SilentlyContinue | ||
Import-Module (Split-Path $PSCommandPath).replace('tests', 'src').Replace('public', 'PSMDE.psd1') | ||
} | ||
|
||
Describe "Update-MdeMachine" { | ||
|
||
It 'Should have the PSMDE module loaded' { | ||
$module = Get-Module PSMDE | ||
$module | Should -Not -BeNullOrEmpty | ||
} | ||
|
||
It 'Should have access to internal functions' { | ||
InModuleScope PSMDE { | ||
$iar = Get-Command Invoke-AzureRequest | ||
$iar | Should -Not -BeNullOrEmpty | ||
} | ||
} | ||
|
||
It 'Should correctly create the request uri' { | ||
InModuleScope PSMDE { | ||
Mock Invoke-RetryRequest { return @{uri = $uri; body = $body } } | ||
Mock Test-MdePermissions { return $true } | ||
$id = '12345' | ||
$tags = @('monitored-a', 'monitored-b') | ||
$priority = 'Normal' | ||
$body = ConvertTo-Json -InputObject @{ machineTags = $tags; deviceValue = $priority } | ||
$result = Update-MdeMachine -id $id -tags $tags -priority $priority | ||
$result.uri | Should -Be "https://api.securitycenter.microsoft.com/api/machines/$id" | ||
$result.body | Should -Be $body | ||
} | ||
} | ||
} |