Last active
August 29, 2015 14:21
-
-
Save Swoogan/f5bafc1baa8825f9b488 to your computer and use it in GitHub Desktop.
PowerShell script to find Octopus Variable tokens that haven't been replaced
This file contains 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
function Find-Unreplaced { | |
<# | |
.SYNOPSIS | |
Looks for Octopus Deploy variables | |
.DESCRIPTION | |
Analyses `Web/App.Release.configs`, etc... looking for Octopus Deploy | |
variables that have not been replaced. | |
.EXAMPLE | |
Find-Unreplaced C:\Folder *.config, *.ps1 | |
.PARAMETER Path | |
Root folder to search in | |
.PARAMETER Files | |
An array of all the files or globs to search in. Defaults to *.config | |
.PARAMETER Exclude | |
Files to ignore | |
.PARAMETER Recurse | |
Should the cmdlet look for the file types recursively | |
.PARAMETER TreatAsError | |
Will cause the script to write an Error instead of a warning if variables are found | |
#> | |
[CmdletBinding()] | |
param | |
( | |
[Parameter( | |
Position=0, | |
Mandatory=$true | |
)] | |
[string] $Path, | |
[Parameter( | |
Position=1, | |
Mandatory=$false | |
)] | |
[string[]] $Files = @('*.config'), | |
[Parameter(Mandatory=$false)] | |
[string[]] $Exclude, | |
[Parameter(Mandatory=$false)] | |
[switch] $Recurse, | |
[Parameter(Mandatory=$false)] | |
[switch] $TreatAsError | |
) | |
process { | |
if (Test-Path $Path -PathType container) { | |
if ($Path.EndsWith("\")) { | |
$Path += "*" | |
} else { | |
$Path += "\*" | |
} | |
} | |
$clean = $true | |
$found = Get-ChildItem -Path $Path -Recurse:$Recurse -Include $Files -Exclude $Exclude -File | |
foreach ($file in $found) { | |
$matches = Select-String -Path $file -Pattern "#\{([^}]*)\}" -AllMatches | |
$clean = $clean -and ($matches.Count -eq 0) | |
Write-Output $matches | |
} | |
if (-not $clean) { | |
$msg = "Unreplaced Octopus Variables were found." | |
if ($TreatAsError) { | |
Write-Error $msg | |
} else { | |
# Like writing a warning in Octopus. Writes to StandardError without | |
# returning a non-zero exit code from the script | |
$host.ui.WriteErrorLine($msg) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment