如何为现有PowerShell脚本添加配置并适配Azure Data Factory对账?
增强版Azure资源配置对账脚本
功能升级说明
- 新增外部配置文件支持:通过指定JSON配置文件路径,灵活定义需对账的资源及预期配置值,避免硬编码
- 新增Azure Data Factory支持:可对账ADF的全局参数(Global Parameters)配置,与Function App对账逻辑统一
完整脚本实现
# 安装依赖模块(首次运行需执行) # Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force param( [Parameter(Mandatory=$true)] [string]$ConfigPath # 指定配置文件路径 ) # 登录Azure(若未登录则触发登录流程) if (-not (Get-AzContext)) { Connect-AzAccount } # 读取配置文件 $data = Get-Content -Path $ConfigPath | ConvertFrom-Json # 函数:通过资源组名称子串匹配获取资源组名 function Get-ResourceGroupName($idSubstring) { $resourceGroups = Get-AzResourceGroup | Where-Object { $_.ResourceGroupName -like "*$idSubstring*" } return $resourceGroups.ResourceGroupName } # 函数:获取Function App的应用设置 function Get-FunctionAppConfig($functionAppName, $resourceGroupName) { $appSettings = Get-AzWebApp -Name $functionAppName -ResourceGroupName $resourceGroupName | Select-Object -ExpandProperty SiteConfig.AppSettings return $appSettings } # 函数:获取ADF的全局参数 function Get-ADFGlobalParameters($dataFactoryName, $resourceGroupName) { $adf = Get-AzDataFactoryV2 -Name $dataFactoryName -ResourceGroupName $resourceGroupName return $adf.GlobalParameters } # 遍历资源组处理对账 foreach ($rg in $data.resourceGroups) { # 确定资源组名称:优先使用配置中指定的名称,否则通过子串匹配查找 $resourceGroupName = if ($rg.resourceGroupName) { $rg.resourceGroupName } else { Get-ResourceGroupName -idSubstring $rg.idSubstring } # 处理Azure Function App配置对账 if ($rg.functionApps) { foreach ($app in $rg.functionApps) { $actualConfig = Get-FunctionAppConfig -functionAppName $app.name -resourceGroupName $resourceGroupName foreach ($key in $app.configSettings.PSObject.Properties.Name) { $expectedValue = $app.configSettings.$key $actualValue = $actualConfig | Where-Object { $_.Name -eq $key } | Select-Object -ExpandProperty Value $match = $actualValue -eq $expectedValue [PSCustomObject]@{ ResourceGroup = $resourceGroupName ResourceType = "Azure Function App" ResourceName = $app.name ConfigKey = $key ConfigValue = $actualValue ExpectedValue = $expectedValue Match = $match } | Format-Table -AutoSize } } } # 处理Azure Data Factory全局参数对账 if ($rg.dataFactories) { foreach ($adf in $rg.dataFactories) { $actualParams = Get-ADFGlobalParameters -dataFactoryName $adf.name -resourceGroupName $resourceGroupName foreach ($key in $adf.globalParameters.PSObject.Properties.Name) { $expectedValue = $adf.globalParameters.$key.Value $actualValue = $actualParams.$key.Value $match = $actualValue -eq $expectedValue [PSCustomObject]@{ ResourceGroup = $resourceGroupName ResourceType = "Azure Data Factory" ResourceName = $adf.name ConfigKey = $key ConfigValue = $actualValue ExpectedValue = $expectedValue Match = $match } | Format-Table -AutoSize } } } }
配置文件示例(config.json)
{ "resourceGroups": [ { "resourceGroupName": "prod-rg-01", "functionApps": [ { "name": "prod-order-processing-func", "configSettings": { "WEBSITE_TIME_ZONE": "UTC", "FUNCTIONS_WORKER_RUNTIME": "powershell", "DB_CONNECTION_STRING": "Server=tcp:prod-sql.database.windows.net,1433;..." } } ], "dataFactories": [ { "name": "prod-etl-adf", "globalParameters": { "Environment": { "Value": "Production", "Type": "String" }, "MaxRetryCount": { "Value": 3, "Type": "Int" } } } ] }, { "idSubstring": "dev-rg", "functionApps": [ { "name": "dev-api-func", "configSettings": { "WEBSITE_RUN_FROM_PACKAGE": "1" } } ] } ] }
使用步骤
- 确保已安装Az模块:
Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force - 登录Azure账号:若未登录,脚本会自动触发登录流程
- 执行脚本:
.\Invoke-AzureConfigReconciliation.ps1 -ConfigPath .\config.json
内容的提问来源于stack exchange,提问作者Martin
相关产品推荐
相关产品推荐

