AzDataFactoryV2LinkedServiceDependencies.psm1

<#
     .SYNOPSIS
         This Command provide the list of Linked Services, Datasets and Pipelines that depend on the Linked Services within an Azure Data Factory.
   
    
   
     .DESCRIPTION
         This function provides the list of Linked Services, Datasets and Pipelines that depend on the Linked Services within an Azure Data Factory.
   
    
   
     .PARAMETER ResourceGroupName
     The resource group containing the DataFactory V2
   
    
   
     .PARAMETER DataFactoryName
     The name of the DataFactory V2.
   
    
   
     .PARAMETER LinkedServiceName
     The name of the LinkedService in the DataFactory V2.
   
    
   
     .EXAMPLE
     Get-AzDataFactoryV2LinkedServiceDependencies -ResourceGroupName "<<RGName>>" -DataFactoryName "<<ADFV2Name>>"
     Get-AzDataFactoryV2LinkedServiceDependencies -ResourceGroupName "<<RGName>>" -DataFactoryName "<<ADFV2Name>>" -LinkedServiceName "<<LinkedServiceName>>"
     
#>

Function Get-AzDataFactoryV2LinkedServiceDependencies
{

 

    Param 
    (
    [Parameter(Mandatory=$true)]
    [string] $ResourceGroupName ,
    [Parameter(Mandatory=$true)]
    [string] $DataFactoryName ,
    [Parameter(Mandatory=$false)]
    [string] $LinkedServiceName 

    )


$Regexpattern='"referenceName": "(.*?)",'
IF(!$LinkedServiceName)
{
$AllLinkedService=Get-AzDataFactoryV2LinkedService -ResourceGroupName $ResourceGroupName -DataFactoryName $DataFactoryName 
}
ELSE
{
$AllLinkedService=Get-AzDataFactoryV2LinkedService -ResourceGroupName $ResourceGroupName -DataFactoryName $DataFactoryName -Name $LinkedServiceName
}

$AllDataset=Get-AzDataFactoryV2Dataset -ResourceGroupName $ResourceGroupName -DataFactoryName $DataFactoryName 
$htDependee = @{}


#This iteration is for Linked Services leveraged within Linked Services

$LinkedServiceIter=Get-AzDataFactoryV2LinkedService -ResourceGroupName $ResourceGroupName -DataFactoryName $DataFactoryName 
for ($i=0;$i -lt $LinkedServiceIter.Count ; $i++)
{
$LSMatch=([regex]::Matches($LinkedServiceIter[$i].Properties.AdditionalProperties.typeProperties.ToString(),$Regexpattern).groups|Where-Object Name -Eq '1').value
$LSMatch=$LSMatch |Select -Unique
if($LSMatch -ne $null)
 {


 foreach ($loop in $LSMatch)
 {

if ($htDependee.ContainsKey($loop)){

$htDependee.$loop += $LinkedServiceIter[$i].Name
}
else{
$htDependee[$loop] = ,$LinkedServiceIter[$i].Name 
}
 }

}}



#This iteration is for Linked Services leveraged within Datasets
for ($i=0;$i -lt $AllLinkedService.Count ; $i++)
{
$LSDependantObjects=@()
for ($j=0;$j -lt $AllDataset.Count ; $j++)
{

if($AllDataset[$j].Properties.LinkedServiceName.ReferenceName -eq $AllLinkedService[$i].Name)
  {
  $LSDependantObjects=$LSDependantObjects+$AllDataset[$j].Name
  }
}


if($LSDependantObjects.count -ne 0)
    {
  if ($htDependee.ContainsKey($AllLinkedService[$i].Name))
  {

     FOREACH($LSDep in $LSDependantObjects)
        {
        $htDependee.$AllLinkedService[$i].Name += $LSDep 
        }

}
else{
     FOREACH($LSDep in $LSDependantObjects)
        {
        $htDependee[$AllLinkedService[$i].Name] += ,$LSDep
        }

}
}
}





#This iteration is for Linked Services leveraged within Pipelines

$AllPipeline=Get-AzDataFactoryV2pipeline -ResourceGroupName $ResourceGroupName -DataFactoryName $DataFactoryName 

for ($i=0;$i -lt $AllLinkedService.Count ; $i++)
{
$LSDependantObjects=@()
for ($j=0;$j -lt $AllPipeline.Count ; $j++)
{

if($AllPipeline[$j].Activities.LinkedServiceName.ReferenceName -eq $AllLinkedService[$i].Name)
  {
  $LSDependantObjects=$LSDependantObjects+$AllPipeline[$j].Name
  }

$InnerAdd=$AllPipeline[$j].Activities.AdditionalProperties

if($InnerAdd.count -gt '1'){
for ($n=0;$n -lt $InnerAdd.Count ; $n++)
{
$LSMatch=([regex]::Matches($InnerAdd[$n]["typeProperties"].ToString(),$Regexpattern).groups|Where-Object Name -Eq '1').value
$LSMatch=$LSMatch |Select -Unique

foreach ($ite in $LSMatch)
{ if($ite -eq $AllLinkedService[$i].Name)
  {
  $LSDependantObjects=$LSDependantObjects+$AllPipeline[$j].Name
  }
}

}
}#EndIf
else
{
$LSMatch=([regex]::Matches($InnerAdd["typeProperties"].ToString(),$Regexpattern).groups|Where-Object Name -Eq '1').value
$LSMatch=$LSMatch |Select -Unique
foreach ($ite in $LSMatch)
{ if($ite -eq $AllLinkedService[$i].Name)
  {
  $LSDependantObjects=$LSDependantObjects+$AllPipeline[$j].Name
  }
}
}
 
}



if($LSDependantObjects.count -ne 0)
    {
    
  if ($htDependee.ContainsKey($AllLinkedService[$i].Name))
  {

       FOREACH($LSDep in $LSDependantObjects){
    if ($htDependee[$AllLinkedService[$i].Name] -notcontains $LSDep) 
     

        {

        $htDependee[$AllLinkedService[$i].Name] += $LSDep
        }
     }
 }
else{
     FOREACH($LSDep in $LSDependantObjects){
    if ($htDependee[$AllLinkedService[$i].Name] -notcontains $LSDep) 
     

        {

        $htDependee[$AllLinkedService[$i].Name] += ,$LSDep
        }
     }

}
}
}



for ($i=0;$i -lt $AllLinkedService.Count ; $i++)
{

if($htDependee.ContainsKey($AllLinkedService[$i].Name))
{

write-host  $AllLinkedService[$i].Name 'has' $($htDependee[$AllLinkedService[$i].Name]).Count 'Dependencies'
Write-Host  $AllLinkedService[$i].Name "is used in" $($htDependee[$AllLinkedService[$i].Name]) `n 
}
else
{
write-host $AllLinkedService[$i].Name "has no / 0 Dependency" `n 
}

}
}


Export-ModuleMember -Function Get-AzDataFactoryV2LinkedServiceDependencies