AzSynapseLinkedServiceDependencies.psm1

<#
     .SYNOPSIS
         This Command provide the list of Linked Services, Datasets and Pipelines that depend on the Linked Service within Synapse.
 
 
 
     .DESCRIPTION
         This function provides the list of Linked Services, Datasets and Pipelines that depend on the Linked Service within Synapse.
 
 
 
     .PARAMETER WorkspaceName
     The WorkspaceName of Synapse
 
 
 
     .PARAMETER LinkedServiceName
     The name of the Linked Service in Synapse.
 
 
 
     .EXAMPLE
     Get-AzSynapseLinkedServiceDependencies -WorkspaceName "<<WorkspaceName>>"
     Get-AzSynapseLinkedServiceDependencies -WorkspaceName "<<WorkspaceName>>" -LinkedServiceName "<<LinkedServiceName>>"
 
#>

Function Get-AzSynapseLinkedServiceDependencies
{

 

 

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

 


$htDependee=@{}
$LS=Get-AzSynapseLinkedService -WorkspaceName $WorkspaceName 
$AllDataset=Get-AzSynapseDataset -WorkspaceName $WorkspaceName 

#This iteration is for Linked Services leveraged within Linked Services
for ($i=0;$i -lt $ls.Count ; $i++)
{
$LSDependant=@()
$pls=$ls[$i].Properties.psobject.Properties| Where-Object -Property TypenameOfValue -LIKE '*AzureKeyVaultSecretReference'
$plsv=$ls[$i].Properties.psobject.Properties| Where-Object -Property value -LIKE '*AzureKeyVaultSecretReference'


if($ls[$i].Properties.ConnectionString) #NOT all Linked services have a connection string
{
if($LS.Name -contains $ls[$i].Properties.ConnectionString["store"].referenceName)
{

$LSDependant=$LSDependant+$ls[$i].Properties.ConnectionString["store"].referenceName 

}
}

for ($j=0;$j -lt $pls.count ;$j++)
{

$m=$pls[$j].Name
$LSIter=$ls[$i].Properties.$m.store.ReferenceName
foreach ($Loop in $LSIter)
{
if($LS.Name -contains $Loop)
{

$LSDependant=$LSDependant+$Loop
}

}


} #end for $j


for ($k=0;$k -lt $plsv.count ;$k++)
{

$mv=$plsv[$k].Name
$LSIter=$ls[$i].Properties.$mv.store.ReferenceName
foreach ($Loop in $LSIter)
{
if($LS.Name -contains $Loop)
{

$LSDependant=$LSDependant+$Loop
}

}


} #end for $k

$LSDependant= $LSDependant |SELECT -Unique
if($LSDependant -ne $null)
 {


 foreach ($loop in $LSDependant)
 {

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

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

}

}



#This iteration is for Linked Services leveraged within Datasets
IF(!$LinkedServiceName)
{
$AllLinkedService=Get-AzSynapseLinkedService -WorkspaceName $WorkspaceName
}
ELSE
{
$AllLinkedService=Get-AzSynapseLinkedService -WorkspaceName $WorkspaceName -Name $LinkedServiceName
}



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-AzSynapsepipeline -WorkspaceName $WorkspaceName

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

$LSMatch=$LSMatch+$AllPipeline[$j].Activities.LinkedServiceName.ReferenceName+$AllPipeline[$j].Activities.IfTrueActivities.LinkedServiceName.ReferenceName+$AllPipeline[$j].Activities.IfFalseActivities.LinkedServiceName.ReferenceName+$AllPipeline[$j].Activities.DefaultActivities.LinkedServiceName.ReferenceName
$LSMatch=$LSMatch+$AllPipeline[$j].Activities.Cases.Activities.LinkedServiceName.ReferenceName+$AllPipeline[$j].Activities.Activities.LinkedServiceName.ReferenceName+$AllPipeline[$j].Activities.Activities.IfTrueActivities.LinkedServiceName.ReferenceName+$AllPipeline[$j].Activities.Activities.IfFalseActivities.LinkedServiceName.ReferenceName
$LSMatch=$LSMatch+$AllPipeline[$j].Activities.Activities.DefaultActivities.LinkedServiceName.ReferenceName+$AllPipeline[$j].Activities.Activities.Cases.Activities.LinkedServiceName.ReferenceName


$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-AzSynapseLinkedServiceDependencies