Functions/Remove-MCASSubnetCollection.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
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
<#
.Synopsis
   Removes a subnet collection in MCAS, as specified by its unique id
.DESCRIPTION
   Remove-MCASSubnetCollection deletes subnet collections in the MCAS tenant.
 
.EXAMPLE
    PS C:\> Remove-MCASSubnetCollection -Identity '5a9e04c7f82b1bb8af51c7fb'
 
.EXAMPLE
    PS C:\> Get-MCASSubnetCollection | Remove-MCASSubnetCollection
 
.FUNCTIONALITY
   Remove-MCASSubnetCollection is intended to remove the specified subnet collection from the MCAS tenant.
#>

function Remove-MCASSubnetCollection {
    [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,

        [Parameter(ParameterSetName='ById',Mandatory=$true,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true,Position=0)]
        [ValidateNotNullOrEmpty()]
        [ValidatePattern("[a-z0-9]{24}")]
        [alias("_id")]
        [string]$Identity,

        [Parameter(ParameterSetName='ByName',Mandatory=$true,ValueFromPipeline=$false)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        [Parameter(Mandatory=$false)]
        [Switch]$Quiet
    )

    process {
        if ($PSCmdlet.ParameterSetName -eq 'ByName') {
            Write-Verbose "Parameter set 'ByName' detected"

            Get-MCASSubnetCollection -Credential $Credential | ForEach-Object {
                if ($_.Name -eq $Name) {
                    $SubnetId = $_.Identity
                    $NameOrIdTargeted = $Name
                }
            }
        }
        elseif ($PSCmdlet.ParameterSetName -eq 'ById') {
            Write-Verbose "Parameter set 'ById' detected"
            $SubnetId = $Identity
            $NameOrIdTargeted = $SubnetId
        }
        else {
            Write-Verbose "Parameter set not detected"
            Write-Error "Could not determine identity of subnet to be deleted"
        }

        try {
            $response = Invoke-MCASRestMethod -Credential $Credential -Path "/api/v1/subnet/$SubnetId/" -Method Delete 
        }
        catch {
            throw "Error calling MCAS API. The exception was: $_"
        }
        
        if (!$Quiet) {
            $Success
        }      
    }
}