Skip to content

Instantly share code, notes, and snippets.

@aaronsb
Created October 28, 2021 18:41
Show Gist options
  • Save aaronsb/f01af2b3b336d4545acf84bed1235d87 to your computer and use it in GitHub Desktop.
Save aaronsb/f01af2b3b336d4545acf84bed1235d87 to your computer and use it in GitHub Desktop.
Simple configuration file editor for parameter = value config file.
function Configure-RasPhoneDNSConfig {
[CmdletBinding()] #we want to use verbose, so add cmdlet binding
param ([string]$path = (Get-Location).path,
[string]$vpnConfigName = "rasphone.pbk",
[string]$configParam = "IpDnsFlags",
[string]$configValue = "1")
#path defaults to current path or another path location.
#vpnconfigname is the name of the file to configure, defaults to rasphone.pbk
#configparam is the parameter we're updating. override it to look for another file name
#configvalue is defaulting to 1
#you could use validate parameters or even a more complex validation scheme that calls another function to validate against a library
trap {
Write-Error $Error[0]
throw "Error encountered. Exiting."
}
#trap any other errors that are not explicit to the try statements
Write-Verbose ("Searching path " + $path + " recursively for " + $VPNConfigName)
#get the file starting at the path
#if you give garbage path, trap out and then with no file list, not do anything. (basic error handlign here, always better to include more robust handling)
#gci recursively from begining of path location.
$fileList = $null #just make filelist variable null for sanitary sake
$fileList = (Get-ChildItem -Recurse -path $path -Filter $VPNConfigName)
Write-Verbose "Recursive search complete"
#instead of .replace method, use a regex filter to catch various permuations of our configuration value.
#This builds a regex string with the configuration parameter.
#The configuration parameter in this regex expects an equal sign = as the value assignment operator.
#The token flag does not impact the regex filter criteria. It is used in the replacement criteria only.
#Below is a serial explaination of the regex string assembly.
#open of regex string -> [']
#opening regex group -> [(]
#beginning of first token name identifier -> [?<]
#first token name -> [flag]
#close token name -> [>]
#close of regex string (incomplete) -> [']
#PS variable in middle of regex string building statement -> [ + $configParam + ]
#reopen of regex string (incomplete) -> [']
#zero or more optional spaces after param name -> [ *]
#zero or more optional spaces after assignment character -> [= *]
#end of first token and beginning of second token -> [)(]
#beginning of second token name identifier -> [?<]
#second token name -> [value]
#match any single word, this is the assigned value -> [\w]
#close the second token -> [)]
#close of regex string -> [']
Write-Verbose "Bulding regex match filter with flag and value tokens"
$paramFilter = [regex]('(?<flag>' + $configParam + ' *= *)(?<value>\w)')
#if filelist has values in it, do some work
if ($fileList) {
#for each file found in the gci
ForEach ($item in $fileList) {
#clear out config file for sanitary code
$configFile = $null
try {
#try and get content of file (sometimes you can list a file but can't read a file)
$configFile = Get-Content $item
#if the contents of the file match the filter parameter in any way, process file
#else, we found a file matching the name, but that file didn't contain the parameter we were looking for
if ($configFile -match $paramFilter) {
Write-Verbose ($item.FullName + ": Updating configuration parameter: " + $configParam + " to value " + $configValue)
try {
#replace the regex string and attempt to save the file
$configFile -replace $paramFilter,('${flag}' + $configValue) | Set-Content -LiteralPath $item.FullName
Write-Verbose ($item.FullName + ": File updated successfully.")
}
catch {
#couldn't save the file for some reason.
Write-Error $Error[0]
}
}
else {
#file didn't have parameters, so nothing to do.
Write-Verbose ($item.FullName + ": Unable to locate parameters: " + $configParam + " for value update to " + $configValue)
}
}
catch {
#print the error out in the case we couldn't:
#1) get the file contents
#2) anything else
Write-Error $Error[0]
}
finally {
#finished inspecting files.
Write-Verbose ("File inspection complete")
}
}
}
else
{
#didn't find any files. no output to console unless verbosely named.
Write-Verbose ("No configuration files found in path " + $path)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment