Skip to content

Instantly share code, notes, and snippets.

@Swoogan Swoogan/Find-Unreplaced
Last active Aug 29, 2015

Embed
What would you like to do?
PowerShell script to find Octopus Variable tokens that haven't been replaced
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
You can’t perform that action at this time.