Created
September 18, 2018 07:06
-
-
Save formerly-chrisfowles/f4bddae942931dbd82ac6118f6189f6a to your computer and use it in GitHub Desktop.
Compare appSettings configured in OctopusDeploy variables and Local *.config files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| param ( | |
| [Parameter(Mandatory=$true)][string]$OctopusDeployApiKey, | |
| [Parameter(Mandatory=$true)][string]$OctopusDeployAddress, | |
| [Parameter(Mandatory=$true)][string]$OctopusDeployProjectId | |
| ) | |
| $ExcludePaths = @('bin', 'Partials', 'Debug', 'Release', 'obj') | |
| $OctopusAuth = @{ | |
| 'X-Octopus-ApiKey' = $OctopusDeployApiKey | |
| } | |
| $OctopusVariables = Invoke-RestMethod -Uri "$OctopusDeployAddress/api/variables/names?project=$OctopusDeployProjectId" -Headers $OctopusAuth | |
| $ConfigFiles = Get-ChildItem -Recurse '*.config' | |
| $Rows = @() | |
| foreach ($File in $ConfigFiles) { | |
| [xml]$XmlDocument = Get-Content $File | |
| $appSettings = $XmlDocument.configuration.appSettings.add | |
| $connectionStrings = $XmlDocument.configuration.connectionStrings.add | |
| $ServiceName = $File.Directory.Name | |
| $FileFullName = $File.FullName.Replace($pwd, '.') | |
| if (($appSettings.keys.Count -gt 0) -and | |
| !((Compare-Object -ReferenceObject $File.Directory.FullName.Split('/').Split('\') -DifferenceObject $ExcludePaths -IncludeEqual).SideIndicator -eq "==") | |
| ) | |
| { | |
| Write-Host "$ServiceName`t" -ForegroundColor Yellow -NoNewline | |
| Write-Host "($FileFullName)" -ForegroundColor Cyan | |
| foreach ($setting in $appSettings) | |
| { | |
| $Key = $setting.key | |
| $Value = $setting.value | |
| if ($OctopusVariables.Contains($Key) -or | |
| $OctopusVariables.Contains("$ServiceName/$Key")) { | |
| Write-Host "`t`t Octopus" -ForegroundColor Green -NoNewline | |
| $Source = 'Octopus' | |
| } | |
| else { | |
| Write-Host "`t`t $($File.Name)" -ForegroundColor DarkBlue -NoNewline | |
| $Source = $File.Name | |
| } | |
| if ($Key.ToLower().Contains('secret') -or | |
| $Key.ToLower().Contains('key') -or | |
| $Key.ToLower().Contains('password') -or | |
| $Key.ToLower().Contains('authtoken')) { | |
| $Value = "*** MASKED ***" | |
| } | |
| Write-Host "`t$Key = $Value" | |
| $Rows += New-Object PSObject -Property @{ | |
| FileName = $FileFullName | |
| ServiceName = $ServiceName | |
| Source = $Source | |
| SettingName = $Key | |
| ConfigValue = $Value | |
| } | |
| } | |
| } | |
| } | |
| $ExportPath = "$Env:TMPDIR/settingexport.csv" | |
| $Rows | Select-Object ServiceName, Source, SettingName, ConfigValue, FileName | Export-Csv -Path $ExportPath | |
| Write-Host "Exported To: $ExportPath" -ForegroundColor Gray |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment