AzDataFactoryV2DatasetDependencies.psm1

<#
     .SYNOPSIS
         This Command provide the list of Dataflows and Pipelines that depend on the Dataset within an Azure Data Factory.
   
    
   
     .DESCRIPTION
         This function provides the list of Dataflow and Pipelines that depend on the Dataset within an Azure Data Factory.
   
    
   
     .PARAMETER ResourceGroupName
     The resource group containing the DataFactory V2
   
    
   
     .PARAMETER DataFactoryName
     The name of the DataFactory V2.
   
    
   
     .PARAMETER DatasetName
     The name of the Dataset in the DataFactory V2.
   
    
   
     .EXAMPLE
     Get-AzDataFactoryV2DatasetDependencies -ResourceGroupName "<<RGName>>" -DataFactoryName "<<ADFV2Name>>"
     Get-AzDataFactoryV2DatasetDependencies -ResourceGroupName "<<RGName>>" -DataFactoryName "<<ADFV2Name>>" -DatasetName "<<DatasetName>>"
     
#>

Function Get-AzDataFactoryV2DatasetDependencies
{

 

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

    )

$Regexpattern='"referenceName": "(.*?)",'
$AllDataflow=Get-AzDataFactoryV2Dataflow -ResourceGroupName $ResourceGroupName -DataFactoryName $DataFactoryName 
$htDependee = @{}


if(!$DatasetName)
{
$AllDataset=Get-AzDataFactoryV2Dataset -ResourceGroupName $ResourceGroupName -DataFactoryName $DataFactoryName 
}
else
{
$AllDataset=Get-AzDataFactoryV2Dataset -ResourceGroupName $ResourceGroupName -DataFactoryName $DataFactoryName -Name $DatasetName
}


#This iteration is for Datasets leveraged within Dataflows and PowerQuery
for ($i=0;$i -lt $AllDataset.Count ; $i++)
{
$DSDependantObjects=@()
for ($j=0;$j -lt $AllDataflow.Count ; $j++)
{ 

 #Dataflows Sources
if($AllDataflow[$j].Properties.Sources.Dataset.ReferenceName -eq $AllDataset[$i].Name)
  {
  $DSDependantObjects=$DSDependantObjects+$AllDataflow[$j].name
  }
  #Dataflows Sinks
if($AllDataflow[$j].Properties.sinks.Dataset.ReferenceName -eq $AllDataset[$i].Name)
  {
  $DSDependantObjects=$DSDependantObjects+$AllDataflow[$j].name
  }
}


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

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

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

}
}
}



#This iteration is for Linked Services leveraged within Pipelines

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

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

if($AllPipeline[$j].Activities.Inputs.ReferenceName -eq $AllDataset[$i].Name)
  {
  $DSDependantObjects=$DSDependantObjects+$AllPipeline[$j].Name
  }

if($AllPipeline[$j].Activities.Outputs.ReferenceName -eq $AllDataset[$i].Name)
  {
  $DSDependantObjects=$DSDependantObjects+$AllPipeline[$j].Name
  }

if($AllPipeline[$j].Activities.Dataset.ReferenceName -eq $AllDataset[$i].Name)
  {
  $DSDependantObjects=$DSDependantObjects+$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 $AllDataset[$i].Name)
  {
  $DSDependantObjects=$DSDependantObjects+$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 $AllDataset[$i].Name)
  {
  $DSDependantObjects=$DSDependantObjects+$AllPipeline[$j].Name
  }
}
}
 
}



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

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

        {

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

        {

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

}
}
}

#PipelineEnd

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

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

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

}
}


Export-ModuleMember -Function Get-AzDataFactoryV2DatasetDependencies