@@ -3,45 +3,75 @@ function Set-CIPPNamedLocation {
33 param (
44 $NamedLocationId ,
55 $TenantFilter ,
6- # $change should be one of 'addip ','addlocation ','removeip ','removelocation '
7- [ValidateSet (' addip ' , ' addlocation ' , ' removeip ' , ' removelocation ' )]
8- $change ,
9- $content ,
6+ # $Change should be one of 'addIp ','addLocation ','removeIp ','removeLocation','rename','setTrusted','setUntrusted','delete '
7+ [ValidateSet (' addIp ' , ' addLocation ' , ' removeIp ' , ' removeLocation ' , ' rename ' , ' setTrusted ' , ' setUntrusted ' , ' delete ' )]
8+ $Change ,
9+ $Content ,
1010 $APIName = ' Set Named Location' ,
1111 $Headers
1212 )
1313
1414 try {
15- $NamedLocations = New-GraphGetRequest - uri " https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations/$NamedLocationId " - Tenantid $tenantfilter
16- switch ($change ) {
17- ' addip' {
18- $NamedLocations.ipRanges = @ ($NamedLocations.ipRanges + @ { cidrAddress = $content ; ' @odata.type' = ' #microsoft.graph.iPv4CidrRange' })
15+ $NamedLocations = New-GraphGetRequest - uri " https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations/$NamedLocationId " - Tenantid $TenantFilter
16+
17+ switch ($Change ) {
18+ ' addIp' {
19+ $NamedLocations.ipRanges = @ ($NamedLocations.ipRanges + @ { cidrAddress = $Content ; ' @odata.type' = ' #microsoft.graph.iPv4CidrRange' })
20+ $ActionDescription = " Adding IP $Content to named location"
21+ }
22+ ' addLocation' {
23+ $NamedLocations.countriesAndRegions = $NamedLocations.countriesAndRegions + $Content
24+ $ActionDescription = " Adding location $Content to named location"
25+ }
26+ ' removeIp' {
27+ $NamedLocations.ipRanges = @ ($NamedLocations.ipRanges | Where-Object - Property cidrAddress -NE $Content )
28+ $ActionDescription = " Removing IP $Content from named location"
29+ }
30+ ' removeLocation' {
31+ $NamedLocations.countriesAndRegions = @ ($NamedLocations.countriesAndRegions | Where-Object { $_ -NE $Content })
32+ $ActionDescription = " Removing location $Content from named location"
1933 }
20- ' addlocation' {
21- $NamedLocations.countriesAndRegions = $NamedLocations.countriesAndRegions + $content
34+ ' rename' {
35+ $NamedLocations.displayName = $Content
36+ $ActionDescription = " Renaming named location to: $Content "
2237 }
23- ' removeip' {
24- $NamedLocations.ipRanges = @ ($NamedLocations.ipRanges | Where-Object - Property cidrAddress -NE $content )
38+ ' setTrusted' {
39+ $NamedLocations.isTrusted = $true
40+ $ActionDescription = ' Setting named location as trusted'
2541 }
26- ' removelocation' {
27- $NamedLocations.countriesAndRegions = @ ($NamedLocations.countriesAndRegions | Where-Object { $_ -NE $content })
42+ ' setUntrusted' {
43+ $NamedLocations.isTrusted = $false
44+ $ActionDescription = ' Setting named location as untrusted'
45+ }
46+ ' delete' {
47+ $ActionDescription = ' Deleting named location'
2848 }
2949 }
30- if ($PSCmdlet.ShouldProcess ($GroupName , " Assigning Application $ApplicationId " )) {
31- # Remove unneeded propertie
32- if ($change -like ' *location*' ) {
33- $NamedLocations = $NamedLocations | Select-Object ' @odata.type' , ' displayName' , ' countriesAndRegions' , ' includeUnknownCountriesAndRegions'
50+
51+ if ($PSCmdlet.ShouldProcess ($NamedLocations.displayName , $ActionDescription )) {
52+ if ($Change -eq ' delete' ) {
53+ $null = New-GraphPOSTRequest - uri " https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations/$NamedLocationId " - tenantid $TenantFilter - type DELETE
54+ $Result = " Deleted named location: $ ( $NamedLocations.displayName ) "
3455 } else {
35- $NamedLocations = $NamedLocations | Select-Object ' @odata.type' , ' displayName' , ' ipRanges' , ' isTrusted'
56+ # PATCH operations - remove unneeded properties
57+ if ($NamedLocations .' @odata.type' -eq ' #microsoft.graph.countryNamedLocation' ) {
58+ $NamedLocations = $NamedLocations | Select-Object ' @odata.type' , ' displayName' , ' countriesAndRegions' , ' includeUnknownCountriesAndRegions'
59+ } elseif ($NamedLocations .' @odata.type' -eq ' #microsoft.graph.ipNamedLocation' ) {
60+ $NamedLocations = $NamedLocations | Select-Object ' @odata.type' , ' displayName' , ' ipRanges' , ' isTrusted'
61+ }
62+
63+ $JsonBody = ConvertTo-Json - InputObject $NamedLocations - Compress - Depth 10
64+ $null = New-GraphPOSTRequest - uri " https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations/$NamedLocationId " - tenantid $TenantFilter - type PATCH - body $JsonBody
65+ $Result = " Edited named location: $ ( $NamedLocations.displayName ) . Change: $Change $ ( if ($Content ) { " with content $Content " }) "
3666 }
3767
38- $null = New-GraphPOSTRequest - uri " https://graph.microsoft.com/beta/identity/conditionalAccess/namedLocations/$NamedLocationId " - tenantid $TenantFilter - type PATCH - body $ ($NamedLocations | ConvertTo-Json - Compress - Depth 10 )
39- Write-LogMessage - headers $Headers - API $APIName - message " Edited named location. Change: $change with content $ ( $content ) " - Sev ' Info' - tenant $TenantFilter
68+ Write-LogMessage - headers $Headers - API $APIName - tenant $TenantFilter - message $Result - Sev ' Info'
4069 }
41- return " Edited named location. Change: $change with content $ ( $content ) "
70+ return $Result
4271 } catch {
4372 $ErrorMessage = Get-CippException - Exception $_
44- Write-LogMessage - headers $Headers - API $APIName - message " Failed to edit named location: $ ( $ErrorMessage.NormalizedError ) " - Sev ' Error' - tenant $TenantFilter - LogData $ErrorMessage
45- return " Failed to edit named location. Error: $ ( $ErrorMessage.NormalizedError ) "
73+ $Result = " Failed to edit named location: $ ( $NamedLocations.displayName ) . Error: $ ( $ErrorMessage.NormalizedError ) "
74+ Write-LogMessage - headers $Headers - tenant $TenantFilter - API $APIName - message $Result - Sev ' Error' - LogData $ErrorMessage
75+ throw $Result
4676 }
4777}
0 commit comments