@@ -229,7 +229,24 @@ function VerifyServiceEndpoints {
229229 return $false
230230 }
231231}
232-
232+ function VerifyServiceDelegation {
233+ param (
234+ $subnet
235+ )
236+ Write-Host (" Verifying Service Delegation for subnet '{0}'." -f $subnet.Name )
237+ If (
238+ (Get-AzDelegation - Subnet $subnet ).ServiceName -eq ' Microsoft.Sql/managedInstances'
239+ )
240+ {
241+ Write-Host " Passed Validation - `' Microsoft.Sql/managedInstances`' delegated to subnet $ ( $subnet.Name ) ." - ForegroundColor Green
242+ return $true
243+ }
244+ Else
245+ {
246+ Write-Host " Warning - `' Microsoft.Sql/managedInstances`' is not delegated to subnet $ ( $subnet.Name ) ." - ForegroundColor Yellow
247+ return $false
248+ }
249+ }
233250function LoadNetworkSecurityGroup {
234251 param (
235252 $subnet
@@ -836,6 +853,23 @@ function PrepareServiceEndpoints
836853 $subnet.ServiceEndpoints.Clear ()
837854}
838855
856+ function PrepareServiceDelegation
857+ {
858+ param ($subnet )
859+ Write-Host " Adding Service Delegation"
860+ Try
861+ {
862+ $provissioingState = Add-AzDelegation - ServiceName " Microsoft.Sql/managedInstances" - Subnet $subnet - Name ' ServiceDelegation'
863+ if ($provissioingState ){
864+ Write-Host " Provissioning State $ ( $provissioingState.ProvisioningState ) "
865+ }
866+ }
867+ Catch
868+ {
869+ Write-Host " Failed: $_ " - ForegroundColor Red
870+ }
871+
872+ }
839873function PrepareNSG
840874{
841875 param (
@@ -938,13 +972,14 @@ $subnet = LoadVirtualNetworkSubnet -virtualNetwork $virtualNetwork -subnetName $
938972Write-Host
939973
940974VerifySubnet $subnet
975+ $isOkServiceDelegation = VerifyServiceDelegation $subnet
941976$isOkServiceEndpoints = VerifyServiceEndpoints $subnet
942977$nsgVerificationResult = VerifyNSG $subnet
943978$isOkNSG = $nsgVerificationResult [' success' ]
944979$routeTableVerificationResult = VerifyRouteTable $subnet
945980$hasRouteTable = $routeTableVerificationResult [' hasRouteTable' ]
946981$isOkRouteTable = $routeTableVerificationResult [' success' ]
947- $isValid = $isOkServiceEndpoints -and $isOkNSG -and $isOkRouteTable
982+ $isValid = $isOkServiceEndpoints -and $isOkNSG -and $isOkRouteTable -and $isOkServiceDelegation
948983
949984If ($isValid -ne $true )
950985{
@@ -974,7 +1009,11 @@ If($isValid -ne $true)
9741009 Write-Host " [UDR] Create Route table with required routes." - ForegroundColor Yellow
9751010 }
9761011 Write-Host " [UDR] Associate newly created Route table to subnet." - ForegroundColor Yellow
977- }
1012+ }
1013+ If ($isOkServiceDelegation -ne $true )
1014+ {
1015+ Write-Host " [Service Delegation] Add Microsoft.Sql/managedInstances as a service delegation for subnet" - ForegroundColor Yellow
1016+ }
9781017 Write-Host
9791018 Write-Host (" -------------------------------------------------------------------------------------------------------- " ) - ForegroundColor Yellow
9801019 Write-Host
@@ -1004,8 +1043,17 @@ If($isValid -ne $true)
10041043 If ($isOkRouteTable -ne $true )
10051044 {
10061045 PrepareRouteTable $routeTableVerificationResult $virtualNetwork $subnet
1007- }
1046+ }
1047+
1048+ If ($isOkNSG -ne $true )
1049+ {
1050+ PrepareNSG $nsgVerificationResult $virtualNetwork $subnet
1051+ }
10081052
1053+ If ($isOkServiceDelegation -ne $true )
1054+ {
1055+ PrepareServiceDelegation $subnet
1056+ }
10091057 SetVirtualNetwork $virtualNetwork
10101058
10111059 Write-Host
0 commit comments