-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRemove-AzWorkspaceManagerAssignment.ps1
More file actions
131 lines (115 loc) · 6.01 KB
/
Remove-AzWorkspaceManagerAssignment.ps1
File metadata and controls
131 lines (115 loc) · 6.01 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
function Remove-AzWorkspaceManagerAssignment {
[cmdletbinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
param (
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[ValidatePattern('^[A-Za-z0-9][A-Za-z0-9-]+[A-Za-z0-9]$', ErrorMessage="It does not match expected pattern '{1}'")]
[Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters.ResourceNameCompleterAttribute(
"Microsoft.OperationalInsights/workspaces",
"ResourceGroupName"
)][string]$WorkspaceName,
[Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
[Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters.ResourceGroupCompleterAttribute()]
[string]$ResourceGroupName,
[Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $false)]
[ValidateNotNullOrEmpty()]
[ValidatePattern('^[A-Za-z0-9][A-Za-z0-9-]+[A-Za-z0-9]$', ErrorMessage="It does not match expected pattern '{1}'")]
[string]$Name,
[Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
[ValidateNotNullOrEmpty()]
[array]$ResourceId,
[Parameter(Mandatory = $false)]
[switch]$Force
)
begin {
Invoke-AzWorkspaceManager -FunctionName $MyInvocation.MyCommand.Name
}
process {
if ($ResourceGroupName) {
$null = Get-AzWorkspaceManager -Name $WorkspaceName -ResourceGroupName $ResourceGroupName
}
else {
$null = Get-AzWorkspaceManager -Name $WorkspaceName
}
if ($Force) {
$ConfirmPreference = 'None'
}
Write-Debug "WorkspaceName: $($WorkspaceName)"
Write-Debug "ResourceGroupName: $($ResourceGroupName)"
Write-Debug "Name: $($Name)"
Write-Debug "ResourceId: $($ResourceId)"
try {
if ($ResourceId) {
$uri = "https://management.azure.com$($ResourceId)?api-version=$($SessionVariables.apiVersion)"
$Name = $ResourceId.Split('/')[-1]
}
elseif ($Name) {
$uri = "$($SessionVariables.workspace)/providers/Microsoft.SecurityInsights/workspaceManagerAssignments/$($Name)?api-version=$($SessionVariables.apiVersion)"
}
else {
Write-Message -FunctionName $($MyInvocation.MyCommand.Name) -Message "No name for the workspace manager assignment was provided" -Severity 'Error'
}
Write-Debug "Performing the operation 'Removing workspace manager assignment'"
Write-Debug "Request URI: $($uri)"
$requestParam = @{
Headers = $authHeader
Uri = $uri
Method = 'GET'
ErrorVariable = 'ErrVar'
}
$apiResponse = Invoke-RestMethod @requestParam
if ($apiResponse -ne '') {
if ($PSCmdlet.ShouldProcess($SessionVariables.workspaceManagerConfiguration -eq 'Enabled', "Remove Workspace Manager Assignment '$Name'")) {
$requestParam = @{
Headers = $authHeader
Uri = $uri
Method = 'DELETE'
ErrorVariable = 'ErrVar'
}
Write-Verbose "Performing the operation 'Removing workspace manager assignment'"
Write-Verbose "Request URI: $($uri)"
Invoke-RestMethod @requestParam
if ($null -eq $response) {
Write-Message -FunctionName $($MyInvocation.MyCommand.Name) -Message "Workspace Manager Assignment '$($Name)' was removed from workspace '$WorkspaceName'" -Severity 'Information'
}
}
}
else {
Write-Message -FunctionName $($MyInvocation.MyCommand.Name) -Message "The Workspace Manager Assignment '$($Name)' does not exist" -Severity 'Error'
}
}
catch {
if ($ErrVar.Message -like '*ResourceNotFound*') {
Write-Message -FunctionName $MyInvocation.MyCommand.Name -Message "Workspace Manager Assignment '$($Name)' was not found under workspace '$WorkspaceName'" -Severity 'Error'
}
else {
Write-Message -FunctionName $($MyInvocation.MyCommand.Name) -Message ($_.Exception.ErrorRecord | ConvertFrom-Json).error.message -Severity 'Error'
}
}
}
<#
.SYNOPSIS
Remove Microsoft Sentinel Workspace Manager Assignment
.DESCRIPTION
The Remove-AzWorkspaceManagerAssignment cmdlet removes a Workspace Manager Assignment from a Microsoft Sentinel Workspace.
The cmdlet will not return an error if the Workspace Manager Assignment does not exist.
The Assignment must first be removed from the Workspace Manager Group before the group can be removed.
.PARAMETER WorkspaceName
The Name of the log analytics workspace
.PARAMETER ResourceGroupName
The name of the ResouceGroup where the log analytics workspace is located
.PARAMETER Name
The Name of the Workspace Manager Assignment
.PARAMETER Force
Confirms the removal of the Workspace manager configuration.
.EXAMPLE
Remove-AzWorkspaceManagerAssignment -WorkspaceName 'myWorkspace' -ResourceGroupName 'myRG' -Name 'myAssignment'
This command removes the Workspace Manager Assignment 'myAssignment' from the workspace 'ContosoWorkspace' in the resource group 'myRG'.
.EXAMPLE
Get-AzWorkspaceManagerAssignment -WorkspaceName 'myWorkspace' | Remove-AzWorkspaceManagerAssignment -Force
This example removes all Workspace Manager Assignments from the workspace 'ContosoWorkspace' in the resource group 'myRG' without prompting for confirmation.
.LINK
Get-AzWorkspaceManagerAssignment
Remove-AzWorkspaceManagerAssignment
#>
}