1+ # Make sure run PowerShell cmd with administrator
2+
3+ param (
4+ [Parameter (Mandatory = $true )]
5+ [string ]$TenantId
6+ )
7+
8+ # Disable warning for get access token
9+ function Get-PlainAccessToken {
10+ $prev = $WarningPreference
11+ $WarningPreference = ' SilentlyContinue'
12+ $token = (Get-AzAccessToken ).Token
13+ $WarningPreference = $prev
14+
15+ $plainToken = [Runtime.InteropServices.Marshal ]::PtrToStringBSTR(
16+ [Runtime.InteropServices.Marshal ]::SecureStringToBSTR($token )
17+ )
18+ return $plainToken
19+ }
20+
21+ function Get-AllLinkedServices {
22+ param (
23+ [string ]$Uri
24+ )
25+
26+ $allLinkedServices = New-Object System.Collections.ArrayList
27+ $nextUri = $Uri
28+ $token = Get-PlainAccessToken
29+
30+ $headers = @ {
31+ Authorization = " Bearer $token "
32+ " Content-Type" = " application/json"
33+ }
34+
35+ while ($nextUri ) {
36+ try {
37+ $response = Invoke-RestMethod - Method GET - Uri $nextUri - Headers $headers
38+ }
39+ catch {
40+ Write-Warning " Failed to fetch: $nextUri "
41+ break
42+ }
43+
44+ if ($response.value -ne $null -and $response.value.Count -gt 0 ) {
45+ # Write-Host " Retrieved $($response.value.Count) linked services"
46+ foreach ($item in $response.value ) {
47+ [void ]$allLinkedServices.Add ($item )
48+ }
49+ }
50+ else {
51+ # Write-Warning "No linked services found in current batch."
52+ break
53+ }
54+
55+ $nextUri = if ($response.PSObject.Properties.Name -contains ' nextLink' ) { $response.nextLink } else { $null }
56+
57+ # # Optional: delay to avoid throttling
58+ # Start-Sleep -Milliseconds 300
59+ }
60+
61+ return $allLinkedServices
62+ }
63+
64+ function Print-Result {
65+ param (
66+ [string ]$subName ,
67+ [string ]$dfName ,
68+ [string ]$lsName ,
69+ [string ]$lsType ,
70+ [string ]$outputFileName
71+ )
72+ Write-Host " $subName , $dfName , $lsName , $lsType "
73+
74+
75+ # Output it to a file
76+ $data = [PSCustomObject ]@ {
77+ SubName = $subName
78+ DataFactoryName = $dfName
79+ LinkedServiceName = $lsName
80+ Type = $lsType
81+ }
82+ $data | Export-Csv - Path $outputFileName - NoTypeInformation - Append
83+ }
84+
85+ # Legacy linked services, we can extend this list
86+ $LegacyV1LSTypes = @ (
87+ # Disabled:
88+ " AmazonMWS" ,
89+ # End of support:
90+ " Zoho" ,
91+ " SalesforceMarketingCloud" ,
92+ " Phoenix" ,
93+ " PayPal" ,
94+ " OracleServiceCloud" ,
95+ " Responsys" ,
96+ " Eloqua" ,
97+ " Marketo" ,
98+ " Magento" ,
99+ " HBase" ,
100+ " Drill" ,
101+ " Couchbase" ,
102+ " Concur" ,
103+ " AzureMariaDB" ,
104+ " GoogleBigQuery" ,
105+ " PostgreSql" ,
106+ " ServiceNow" ,
107+ " Snowflake" ,
108+ " Salesforce" ,
109+ " SalesforceServiceCloud"
110+ )
111+
112+ $LegacyV1VersionLSTypes = @ (
113+ # End of support
114+ " MySql" ,
115+ " MariaDB" ,
116+ # V2 GA
117+ " Vertica" ,
118+ " Oracle" ,
119+ " Greenplum" ,
120+ " AzurePostgreSql" ,
121+ " Teradata" ,
122+ " AmazonRdsForOracle" ,
123+ " Hive" ,
124+ " Impala" ,
125+ " Spark" ,
126+ " Presto" ,
127+ " Cassandra" ,
128+ # V2 Preview
129+ " QuickBooks"
130+ )
131+
132+ # Install/Update Required Modules
133+ Write-Host " Checking for Az modules..." - ForegroundColor Cyan
134+ Install-Module - Name Az.DataFactory - Force - AllowClobber
135+ Import-Module Az.DataFactory
136+ # If you hit incompatible version, try Install-Module -Name Az.Accounts -RequiredVersion <your version>, and reopen the cmd
137+
138+ # Show the version in use
139+ $module = Get-Module - Name Az.DataFactory
140+ if ($module ) {
141+ Write-Host " Az.DataFactory version in use: $ ( $module.Version ) " - ForegroundColor Green
142+ } else {
143+ Write-Host " Az.DataFactory module not loaded." - ForegroundColor Red
144+ }
145+
146+ $timestamp = Get-Date - Format " yyyyMMdd_HHmmss"
147+ $filename = " output_$timestamp .csv"
148+
149+ # Log into Azure
150+ Connect-AzAccount - ErrorAction Stop - tenantId $TenantId
151+
152+ Write-Host " Retrieving Subscriptions..." - ForegroundColor Cyan
153+ $Subscriptions = Get-AzSubscription - tenantId $TenantId
154+
155+ foreach ($sub in $Subscriptions ) {
156+ $SubscriptionId = $sub.Id
157+ $subName = $sub.Name
158+ # Set sub ID
159+ Write-Host " Connecting to sub $SubscriptionId " - ForegroundColor Cyan
160+ Set-AzContext - Subscription $SubscriptionId - TenantId $tenantId | Out-Null
161+
162+ # Get all Data Factories in the subscription
163+ Write-Host " Retrieving Data Factories..." - ForegroundColor Cyan
164+ $dataFactories = Get-AzDataFactoryV2
165+
166+ if ($dataFactories.Count -eq 0 ) {
167+ Write-Host " No Data Factories found in subscription $SubscriptionId "
168+ }
169+
170+ foreach ($df in $dataFactories ) {
171+ $resourceGroup = $df.ResourceGroupName
172+ $dataFactoryName = $df.DataFactoryName
173+
174+ $uri = " https://management.azure.com/subscriptions/$SubscriptionId /resourceGroups/$resourceGroup /providers/Microsoft.DataFactory/factories/$dataFactoryName /linkedservices?api-version=2018-06-01"
175+
176+ try {
177+ $response = Invoke-AzRestMethod - Method GET - Uri $uri
178+ $linkedServices = Get-AllLinkedServices - Uri $uri
179+
180+
181+ if ($linkedServices.Count -eq 0 ) {
182+ continue
183+ }
184+
185+ foreach ($ls in $linkedServices ) {
186+ $name = $ls.name
187+ $type = $ls.properties.type
188+ $version = $ls.properties.version
189+ $typeProps = $ls.properties.typeProperties | ConvertTo-Json - Depth 10 | ConvertFrom-Json
190+
191+ if ($LegacyV1LSTypes -contains $type ) {
192+ Print- Result - subName $subName - dfName $dataFactoryName - lsName $name - lsType $type - outputFileName $filename
193+ }
194+
195+ # Find v1 versions.
196+ if ($LegacyV1VersionLSTypes -contains $type ) {
197+ if ($version -ne " 2.0" ) { # Skip version 2.0, it must be non-legacy
198+ switch ($type ) { # MySql and MariaDB are not following the version design, hence, need some custom logic here
199+ {($_ -eq " MariaDB" ) -or ($_ -eq " MySql" )} {
200+ $connectionString = $typeProps.connectionString
201+ if (-not [string ]::IsNullOrEmpty($connectionString )) {
202+ Print- Result - subName $subName - dfName $dataFactoryName - lsName $name - lsType $type - outputFileName $filename
203+ }
204+ break
205+ }
206+ default {
207+ Print- Result - subName $subName - dfName $dataFactoryName - lsName $name - lsType $type - outputFileName $filename
208+ break
209+ }
210+ }
211+ }
212+ }
213+ }
214+ } catch {
215+ Write-Host " Failed to fetch linked services for $dataFactoryName " - ForegroundColor Red
216+ }
217+ }
218+ }
0 commit comments