Private/ManagedVirtualNetwork/Approve-ManagedPrivateEndpointDataFactory.ps1


Function Approve-ManagedPrivateEndpointDataFactory {

    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [string]$factoryName,
   
        [Parameter(Mandatory = $true)]
        [string]$resourceGroupName,

        [Parameter(Mandatory = $true)]
        [array]$listManagedPrivateEndpoints,
        
        [Parameter(Mandatory = $true)]
        [string]$subscriptionId,
   
        [string]$apiVersion = "2018-06-01"
    )
      
        #iterate through all managed private endpoints
        foreach ($managedPrivateEndpointObject in $listManagedPrivateEndpoints) {
            
            Write-InformationColored -ForegroundColor Green -MessageData "Processing managed private endpoint: $($managedPrivateEndpointObject.name)"

            try {
                #check if the managed private endpoint is created already
                #if not, wait for 5 seconds and try again
                while ($true) {
                    try {
                        $managedPrivateEndpointObject = Get-ManagedPrivateEndpoint -factoryName $factoryName -managedPrivateEndpointName $managedPrivateEndpointObject.name -resourceGroupName $resourceGroupName -subscriptionId $subscriptionId -apiVersion $apiVersion

                        if ($managedPrivateEndpointObject.properties.ProvisioningState) {
                            break
                        }
                        throw 
                    }
                    catch {
                        Start-Sleep -Seconds 10
                        $managedPrivateEndpointObject = Get-ManagedPrivateEndpoint -factoryName $factoryName -managedPrivateEndpointName $managedPrivateEndpointObject.name -resourceGroupName $resourceGroupName -subscriptionId $subscriptionId -apiVersion $apiVersion
                    }

                }


                #check if the managed private endpoint is in succeeded state
                if ($managedPrivateEndpointObject.properties.ProvisioningState -ne "Succeeded") {
                    #Get the private endpoint connection object name
                    $managedPrivateEndpointName = $managedPrivateEndpointObject.name
                    #Get the private endpoint connection object
                    $linkResourceId = $managedPrivateEndpointObject.properties.privateLinkResourceId
                
                    #Find private endpoint connection object
                    $linkResourcePrivateEndpoints = Get-AzPrivateEndpointConnection -PrivateLinkResourceId $linkResourceId
                
                    #check if the private endpoint connection object is created already
                    #if not, wait for 15 seconds and try again
                    while ($true) {
                        try {
                            $privateEndpointObject = ($linkResourcePrivateEndpoints | Where-Object { $_.PrivateLinkServiceConnectionState.Description.Contains($managedPrivateEndpointName) -and $_.PrivateLinkServiceConnectionState.Description.Contains($factoryName) })[0]
                            break
                        }
                        catch {
                            Start-Sleep -Seconds 15
                            $linkResourcePrivateEndpoints = Get-AzPrivateEndpointConnection -PrivateLinkResourceId $linkResourceId
                        }
                    }
                    #check if the private endpoint connection object is in approved state
                    #if not, approve it
                    #if it is already approved, do nothing
                    if ($privateEndpointObject.PrivateLinkServiceConnectionState.Status -ne "Approved") {
                        $a = Approve-AzPrivateEndpointConnection  -ResourceId $privateEndpointObject.Id -Description "Requested by DataFactory:$factoryName, Name:$managedPrivateEndpointName"
                        Write-Log "Requested by DataFactory:$factoryName, Name:$managedPrivateEndpointName is approved"
                    }
                    elseif ($privateEndpointObject.PrivateLinkServiceConnectionState.Status -eq "Approved") {
                        Write-Log "Requested by DataFactory:$factoryName, Name:$managedPrivateEndpointName is already approved"
                    }

                    #check if the managed private endpoint is in succeeded state
                    #if not, wait for 5 seconds and try again
                    while ($true) {
                        $managedPrivateEndpointObject = Get-ManagedPrivateEndpoint -factoryName $factoryName -managedPrivateEndpointName $managedPrivateEndpointObject.name -resourceGroupName $resourceGroupName -subscriptionId $subscriptionId -apiVersion $apiVersion
                
                        if ($managedPrivateEndpointObject.properties.ProvisioningState -eq "Succeeded") {
                            Write-Log "ManagedPrivateEndpoint $managedPrivateEndpointName is approved"
                            Write-InformationColored -ForegroundColor Green -MessageData "ManagedPrivateEndpoint $managedPrivateEndpointName is approved"
                            break
                        }
                        elseif ($managedPrivateEndpointObject.properties.ProvisioningState -eq "Failed") {
                            Write-Log "ManagedPrivateEndpoint $managedPrivateEndpointName is failed"
                            throw 
                        }
                        else {
                            Start-Sleep -Seconds 15
                        }
                    }
        
                }else{
                    Write-InformationColored -ForegroundColor Green -MessageData "ManagedPrivateEndpoint $($managedPrivateEndpointObject.name) is already in succeeded state"

                }
                
            }
            catch {
                Write-InformationColored -ForegroundColor Red -MessageData "Error processing managed private endpoint: $($managedPrivateEndpointObject.name) - $_"
            }
        }
}