param( [Parameter(Mandatory=$True)][string] $subscriptionId, [Parameter(Mandatory=$True)][string] $resourceGroupName ) Login-AzureRmAccount $env:ScriptLocation = Get-Location $rootPath = $env:ScriptLocation Set-AzureRmContext -SubscriptionId $subscriptionId $resourcesObject=New-Object -TypeName PSObject $resourcesObject=@{} $webApps=Get-AzureRmWebApp -ResourceGroupName $resourceGroupName foreach($webApp in $webApps) { $webAppName=$webApp.Name $fileName="ConnectionStrings.config" $kuduApiUrl = "https://"+$webAppName+".scm.azurewebsites.net/api/vfs/site/wwwroot/App_Config/$fileName" Write-Host "Getting Webapp $webAppName credentials" $resourceType = "Microsoft.Web/sites/config" $resourceName = "$webAppName/publishingcredentials" $publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType ` -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force $kuduApiAuthorisationToken = ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword)))) $filePath="$rootPath\$fileName" try{ Invoke-RestMethod -Uri $kuduApiUrl ` -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} ` -Method GET ` -ContentType "xml" ` -OutFile $filePath ` -ErrorAction SilentlyContinue }catch{} [xml]$xmlResult=Get-Content $filePath $webAppObject = New-Object -TypeName PSObject $webAppObject | Add-Member -Name 'ConnectionStrings' -MemberType NoteProperty -Value (New-object System.Collections.Arraylist) $webAppObject | Add-Member -Name 'ReferencedWebAppsComponents' -MemberType NoteProperty -Value (New-Object System.Collections.Arraylist) $webAppObject | Add-Member -Name 'WebAppName' -MemberType Noteproperty -Value '' $webAppObject | Add-Member -Name 'OutboundIpAddresses' -MemberType Noteproperty -Value '' $webAppObject.WebAppName=$webAppName Write-Host "Getting web app $webAppName connection strings for databases and referenced components" -ForegroundColor DarkYellow foreach( $connectionStringNode in $xmlResult.connectionStrings.add) { if($connectionStringNode.connectionString.StartsWith("http")) { $referencedWebApp=[PSCustomObject]@{ 'ConnectionStringName'=$connectionStringNode.name 'WebAppUrl'=$connectionStringNode.connectionString 'WebAppName'=$connectionStringNode.connectionString.Replace("https://","").Replace(".azurewebsites.net","") } if($referencedWebApp.WebAppName -ne $null) { $j=$webAppObject.ReferencedWebAppsComponents.add($referencedWebApp) } } elseif($connectionStringNode.name.StartsWith("reporting.apikey") -and ($webApp.Name.EndsWith("rep") -ne $True)) { $reportingWebApp=$webApps | Where-Object { $_.Name.EndsWith("rep") -and ($_.Name.EndsWith("ma-rep") -eq $false)} $repotingWebAppName=$reportingWebApp.ResourceName $reportingWebAppURL="https://$repotingWebAppName.azurewebsites.net" $referencedWebApp=[PSCustomObject]@{ 'ConnectionStringName'=$connectionStringNode.name 'WebAppUrl'=$reportingWebAppURL 'WebAppName'=$repotingWebAppName } if($referencedWebApp.WebAppName -ne $null) { $j=$webAppObject.ReferencedWebAppsComponents.add($referencedWebApp) } } else { $sb = New-Object System.Data.Common.DbConnectionStringBuilder try { $sb.set_ConnectionString($connectionStringNode.connectionString) $connectionString=[PSCustomObject]@{ 'name'=$connectionStringNode.name 'initialcatalog'=$sb.'initial catalog' 'datasource'=$sb.'data source' } if($connectionString.initialcatalog -ne $null){ $i=$webAppObject.ConnectionStrings.Add($connectionString) } } Catch { } } } write-host "Getting web app $webAppName Outbound Ip Addresses" -ForegroundColor DarkYellow $webAppObject.OutboundIpAddresses=(Get-AzureRmWebApp -Name $webAppName -ResourceGroupName $resourceGroupName).OutboundIpAddresses $resourcesObject.Add($webAppName,$webAppObject) } $outputFile= $rootPath+"\WebAppConnectionStrings.json" $resourcesObject | ConvertTo-Json -Depth 4 | Out-File $outputFile Write-Host "Script completed successfuly" -ForegroundColor Green Write-Host "Find the results in $outputFile" -ForegroundColor Green