Functions/Remove-MCASDiscoveryDataSource.ps1

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
function Remove-MCASDiscoveryDataSource {
    [CmdletBinding()]
    param
    (
        # Specifies the credential object containing tenant as username (e.g. 'contoso.us.portal.cloudappsecurity.com') and the 64-character hexadecimal Oauth token as the password.
        [Parameter(Mandatory=$false)]
        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.PSCredential]$Credential = $CASCredential,

        # Specifies the name of the data source object to create
        [Parameter(Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0)]
        [alias("_id")]
        [string]$Identity
    )
    begin {
        Write-Verbose "Checking current data sources"
        $currentDataSources = Get-MCASDiscoveryDataSource -Credential $Credential
    }
    process {
        if ($currentDataSources.Identity.Contains($Identity)) {
            if (($currentDataSources | Where-Object {$_.Identity -eq $Identity}).receiverType -ne 'builtin') {
                try {
                    $response = Invoke-MCASRestMethod -Credential $Credential -Path "/api/v1/discovery/data_sources/$Identity/" -Method Delete
                }
                catch {
                    throw "Error calling MCAS API. The exception was: $_"
                }
            }
            else {
                Write-Warning "The data source with id $Identity is built-in and cannot be removed. It will be skipped."    
            }
        }
        else {
            Write-Warning "There is no data source with the id of $Identity. No changes were made."
        }   
    }
    end {
    }
}