Skip to content

Commit 6924148

Browse files
Clare Zheng (Shanghai Wicresoft Co Ltd)Clare Zheng (Shanghai Wicresoft Co Ltd)
authored andcommitted
Add find impacted objects PowerShell script
1 parent 557a0cc commit 6924148

File tree

1 file changed

+244
-0
lines changed

1 file changed

+244
-0
lines changed

Connector/FindImpactedObjects.ps1

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

0 commit comments

Comments
 (0)