Functions/Set-MCASSubnetCollection-ALPHA.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
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
132
133
134
135
136
137
138
function Set-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(Mandatory=$true, Position=0)]
        [ValidateNotNullOrEmpty()]
        [ValidatePattern("[a-z0-9]{24}")]
        [alias("_id")]
        [string]$Identity,
    
        [Parameter(Mandatory=$false,ValueFromPipeline=$true,Position=1)]
        [ValidateNotNullOrEmpty()]
        [string]$Name,

        [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=1)]
        [ValidateNotNullOrEmpty()]
        [ip_category]$Category,

        [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=2)]
        [ValidateNotNullOrEmpty()]
        [ValidatePattern('^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}\/\d{1,2}$|^[a-zA-Z0-9:]{3,39}\/\d{1,3}$')]
        [string[]]$Subnets,

        [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=4)]
        [ValidateNotNullOrEmpty()]
        [string]$Organization,

        [Parameter(Mandatory=$false,ValueFromPipelineByPropertyName=$true,Position=5)]
        [ValidateNotNullOrEmpty()]
        [string[]]$Tags,

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

    process {
        # Get the object by its id
        $item = Get-MCASSubnetCollection -Credential $Credential | Where-Object {$_._id -eq $Identity}



        
        
        Write-Host -ForegroundColor Cyan "Before:"
        $item
        
        

        

        # Modify the object properties based on params provided
        if ($Name) {
            $item.name = $Name
        }
        
        if ($Category) {
            $item.category = $Category
        }

        if ($Subnets) {
            $item.subnets = $Subnets
        }

        if ($Tags) {
            $item.tags = $Tags
        }

        if ($Organization) {
            $item.organization = $Organization
        }

        # Fixup any properties that need fixing
        if ($item.tags -eq (@{})) {
            $item.tags = $null
        }
        #$item.tags = $null





        Write-Host -ForegroundColor Cyan "After:"
        $item

        
        
        
        
        # Convert the object into a hashtable, then a JSON document
        $body = @{}
        
        $item.psobject.properties | ForEach-Object {$body.Add($_.Name,$_.Value) }
        
        $body = $body | ConvertTo-Json -Compress -Depth 3


        

        Write-Host -ForegroundColor Cyan "Body:"
        $body







        try {
            $response = Invoke-MCASRestMethod -Credential $Credential -Path "/cas/api/v1/subnet/$Identity/update_rule/" -Method Post -Body $body
        }
        catch {
            throw "Error calling MCAS API. The exception was: $_"
        }
        
        <#
        Write-Verbose "Checking response for success"
        if ($response.StatusCode -eq '200') {
            Write-Verbose "Successfully modified subnet $NameOrIdTargeted"
        }
        else {
            Write-Verbose "Something went wrong attempting to modify subnet $NameOrIdTargeted"
            Write-Error "Something went wrong attempting to modify subnet $NameOrIdTargeted"
        }
        #>


        #$response = $response.content | ConvertFrom-Json

        if (!$Quiet) {
            $response
        }
    }
}